2012-05-11 57 views
0

您好我目前有一個活動,承載一個選項卡視圖,每個選項卡是一個活動。目前我遇到的問題是,在其中一項活動(選項卡)中,我有一些東西需要在旋轉時保存,但似乎這些方法在我更改旋轉時不會被調用,只有保存選項卡視圖並啓動的活動每個活動作爲一個選項卡。活動作爲選項卡獲取onSaveInstanceState/onRestoreInstanceState方法調用旋轉?

下面是活動的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. 

12月5日至11日:15:1603.3:W /包(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):at android.app.LocalActivityManage r.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):at android。 app.Activity.performCreate(Activity.java:4465) 05-11 12:15:16.033:W/Bundle(32053):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 05-11 12: 15:16.033:W/Bundle(32053):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 05-11 12:15:16.033:W/Bundle(32053):at android.app.ActivityThread。 handleLaunchActivity(ActivityThread.java:1981) 05-11 12:15:16.033:W/Bundle(32053):at android.app.ActivityThread.ha ndleRelaunchActivity(ActivityThread.java:3351) 05-11 12:15:16.033:W/Bundle(32053):at android.app.ActivityThread.access $ 700(ActivityThread.java:123) 05-11 12:15:16.033 :W/Bundle(32053):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1151) 05-11 12:15:16.033:W/Bundle(32053):at android.os.Handler.dispatchMessage (Handler.java:99) 05-11 12:15:16.033:W/Bundle(32053):at android.os.Looper.loop(Looper.java:137) 05-11 12:15:16.033:W/Bundle(32053):at android.app.ActivityThread.main(ActivityThread.java:4424) 05-11 12:15:16.033:W/Bundle(32053):at java.lang.reflect.Method.invokeNative(Native方法) 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( 3205 3):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) 12月5日至11日:15:16.033:W /軟件包(32053):在dalvik.system.NativeStart.main(本機方法)

其指的: mlam。的DispatchCreate(savedInstanceState);

回答

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" 
} 
+0

這些調用是按照保存/恢復數據的順序嗎? – Maxrunner

+0

你有沒有嘗試過...... –

+0

是啊似乎沒有工作,但我想你的意思是把這些活動作爲選項卡正確???實際上我把它們放在兩個類,但只有與選項卡視圖的活動是獲取這些方法稱爲... – Maxrunner

相關問題