开发者

How to respond for onConfigChanges in one of my tabs?

开发者 https://www.devze.com 2023-03-23 13:28 出处:网络
I have tab layout in portrait mode. Now I have to respond for onConfigChanges happened in one of my tabs. Following research on google I made this code:

I have tab layout in portrait mode. Now I have to respond for onConfigChanges happened in one of my tabs. Following research on google I made this code:

manifest.xml

<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name=".aaa_bbb" android:label="@string/app_name"
              android:theme="@android:style/Theme.NoTitleBar"
              android:screenOrientation="portrait"
              android:windowSoftInputMode="stateUnchanged|adjustPan"
              android:configChanges="keyboardHidden">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name="aaa">
    </activity>
    <activity android:name="bbb">
      <intent-filter>
            <action android:name="com.mycompany.myApp.KEYBOARD" />
        </intent-filter>
    </activity>
</application>
<uses-permission android:name="android.permission.WRITE_SETTINGS" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

My main activity:

public class aaa_bbb extends TabActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    Resources res = getResources(); // Resource object to get Drawables
    TabHost tabHost = getTabHost();  // The activity TabHost
    TabHost.TabSpec spec;  // Reusable TabSpec for each tab
    Intent intent;  // Reusable Intent for each tab

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, aaa.class);
    // Initialize a TabSpec for each tab and add it to the TabHost
    spec = tabHost.newTabSpec("aaa");
    spec.setIndicator("Aaa",res.getDrawable(R.drawable.tab_aaa));
    spec.setContent(intent);
    tabHost.addTab(spec);

    // Do the same for the other tabs
    intent = new Intent(this, bbb.class); /* just different way to do the same job */
    spec = tabHost.newTabSpec("bbb");
    spec.setIndicator("Bbb", res.getDrawable(R.drawable.tab_bbb));
    spec.setContent(intent);
    tabHost.addTab(spec);

    tabHost.setCurrentTab(0);
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    Toast.makeText(getBaseContext(), "Config changes", Toast.LENGTH_LONG).show();
    Intent i = new Intent();
    i.setAction("com.mycompany.myApp.KEYBOARD");
    sendBroadcast(i);
}
}

And finally in my tab class (activity):

public class bbb extends Activity {
private MyListener listener = null;
private Boolean listener_is_registered = false;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bbb);

    listener = new MyListener();
}

@Override
public void onResume() {
    super.onResume();

    if (!listener_is_registered) {
        registerReceiver(listener, new IntentFilter("com.mycompany.myApp.KEYBOARD"));
        listener_is_registered = true;
        Toast.makeText(getBaseContext(), "Listener registered true.", Toast.LENGTH_LONG).show();
    }
}

@Override
public void onPause() {
    super.onPause();

    if (listener_is_registered) {
        unregisterReceiver(listener);
        listener_is_registered = false;
        Toast.makeText(getBaseContext(), "Listener registered false", Toast.LENGTH_LONG).show();
    }
}


// Nested 'listener'
protected class MyL开发者_JAVA技巧istener extends BroadcastReceiver {
    @Override
    public void onReceive(Context context, Intent intent) {
        // No need to check for the action unless the listener will
        // will handle more than one - let's do it anyway
        if (intent.getAction().equals("com.mycompany.myApp.KEYBOARD")) {
            /* perform some action */
            Toast.makeText(getBaseContext(), "Intent reciewied", Toast.LENGTH_LONG).show();
        }
    }
}
}

I don't known where I did mistake, but my bbb activity still do not get response for keyboard hide or show.


Your not figuring out the orientation properly. Do you want to do some other action after going to a certain orientation or just hide keyboards? Something like this adding booleans to help keep track would probably help:

public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);

    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {


        isHorizontal=true;

    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT){

        isVertical=true;
    }
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号