0

嗨,我目前有一个承载选项卡视图的活动,每个选项卡都是一个活动。我当前遇到的问题是,在其中一个活动(选项卡)上,我需要在旋转时保存一些东西,但是当我更改旋转时,似乎这些方法不会被调用,仅在包含选项卡视图和启动的活动上每个活动作为一个选项卡。

这是包含选项卡的活动的 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): at 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): 在 sinfic.mobile.ipdms.MainMenu.initActivity(MainMenu. java:66) 05-11 12:15:16.033: W/Bundle(32053): 在 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): 在 java.lang.reflect.Method.invokeNative(Native Method) 05-11 12:15:16.033: W/Bundle(32053): 在 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): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-11 12:15:16.033: W/Bundle( 32053):在 dalvik.system.NativeStart.main(本机方法)在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-11 12:15:16.033: W/Bundle(32053): 在 dalvik.system.NativeStart.main(Native Method)在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 05-11 12:15:16.033: W/Bundle(32053): 在 dalvik.system.NativeStart.main(Native Method)

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

4

1 回答 1

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"
}
于 2012-05-11T10:47:17.237 回答