作为选项卡的活动是否在旋转时调用它们的onSaveInstanceState / onRestoreInstanceState方法?

时间:2012-05-11 10:41:16

标签: android tabs rotation

您好我目前有一个托管标签视图的活动,每个标签都是一个活动。我当前的问题是,在其中一个活动(选项卡)上我有一些东西我需要保存旋转,但似乎这些方法在我改变旋转时不会被调用,只有在保持选项卡视图和启动的活动上每个活动作为标签。

这是包含标签的活动的onCreate方法:

LocalActivityManager mlam = new LocalActivityManager(this, false);
    tabs=(TabHost)findViewById(android.R.id.tabhost);
    mlam.dispatchCreate(savedInstanceState);
    tabs.setup(mlam);

    TabHost.TabSpec spec;
    Intent intent;

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, MyProcessList.class);
    spec = tabs.newTabSpec("artists").setIndicator((buildTabIndicator("Home")))
            .setContent(intent);
    tabs.addTab(spec);

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, MyProcessList.class);
    spec = tabs.newTabSpec("artists2").setIndicator((buildTabIndicator("Menu")))
    .setContent(intent);

    // Create an Intent to launch an Activity for the tab (to be reused)
    intent = new Intent().setClass(this, MyProcessList.class);
    tabs.addTab(spec);
    spec = tabs.newTabSpec("artists2").setIndicator((buildTabIndicator("Options")))
    .setContent(intent);

    tabs.addTab(spec);


    tabs.setCurrentTab(0);`

同一类中的onSaveInstanceState / onRestoreInstanceState方法:

    @Override
public void onSaveInstanceState(Bundle bundle) {

    bundle.putString(BOB,"ola");

}

@Override
public void onRestoreInstanceState(Bundle bundle) {

    String hi=bundle.getString(BOB);
    System.out.println(hi);


}

但作为标签的活动(MyProcessList)拥有的onSaveInstanceState / onRestoreInstanceState方法似乎没有被调用......有没有办法解决这个问题?

的问候,

以下是使用MainMenu活动中的方法时收到的警告:

05-11 12:15:16.017: W/Bundle(32053): Key BOB expected Bundle but value was a java.lang.String.  The default value <null> was returned.

05-11 12:15:16.033:W / Bundle(32053):尝试转换生成的内部异常: 05-11 12:15:16.033:W / Bundle(32053):java.lang.ClassCastException:java.lang.String无法强制转换为android.os.Bundle 05-11 12:15:16.033:W / Bundle(32053):在android.os.Bundle.getBundle(Bundle.java:1142) 05-11 12:15:16.033:W / Bundle(32053):在android.app.LocalActivityManager.dispatchCreate(LocalActivityManager.java:455) 05-11 12:15:16.033:W / Bundle(32053):at sinfic.mobile.ipdms.MainMenu.initActivity(MainMenu.java:66) 05-11 12:15:16.033:W / Bundle(32053):at sinfic.mobile.ipdms.core.IpdmsCoreActivity.onCreate(IpdmsCoreActivity.java:38) 05-11 12:15:16.033:W / Bundle(32053):在android.app.Activity.performCreate(Activity.java:4465) 05-11 12:15:16.033:W / Bundle(32053):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 05-11 12:15:16.033:W / Bundle(32053):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 05-11 12:15:16.033:W / Bundle(32053):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 05-11 12:15:16.033:W / Bundle(32053):在android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3351) 05-11 12:15:16.033:W / Bundle(32053):在android.app.ActivityThread.access $ 700(ActivityThread.java:123) 05-11 12:15:16.033:W / Bundle(32053):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1151) 05-11 12:15:16.033:W / Bundle(32053):在android.os.Handler.dispatchMessage(Handler.java:99) 05-11 12:15:16.033:W / Bundle(32053):在android.os.Looper.loop(Looper.java:137) 05-11 12:15:16.033:W / Bundle(32053):在android.app.ActivityThread.main(ActivityThread.java:4424) 05-11 12:15:16.033:W / Bundle(32053):at java.lang.reflect.Method.invokeNative(Native Method) 05-11 12:15:16.033:W / Bundle(32053):at java.lang.reflect.Method.invoke(Method.java:511) 05-11 12:15:16.033:W / Bundle(32053):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:784) 05-11 12:15:16.033:W / Bundle(32053):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-11 12:15:16.033:W / Bundle(32053):at dalvik.system.NativeStart.main(Native Method)

它指的是: mlam.dispatchCreate(savedInstanceState);

1 个答案:

答案 0 :(得分:1)

我认为你也需要打电话给超级。

@Override
protected void onSaveInstanceState(Bundle outState)
{
    // SAVE YOUR DATA IN "outState"
    super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    //RECOVER YOUR DATA FROM "savedInstanceState"
}