我的应用中有两个标签的tabHost活动。我从标签1开始。如果我按下Android的后退按钮,我可以转到前一个活动。当我转到第二个标签并按下后退按钮时,我会回到之前的活动中。
如果我再次切换标签(所以再次在第一个标签页上),我收到以下错误:
01-04 11:37:32.639: E/AndroidRuntime(2056): FATAL EXCEPTION: main
01-04 11:37:32.639: E/AndroidRuntime(2056): Process: com.example.damir.klicker, PID: 2056
01-04 11:37:32.639: E/AndroidRuntime(2056): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1365)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.support.v4.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:500)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.support.v4.app.FragmentActivity.onBackPressed(FragmentActivity.java:176)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.app.Activity.onKeyUp(Activity.java:2201)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.KeyEvent.dispatch(KeyEvent.java:2664)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.app.Activity.dispatchKeyEvent(Activity.java:2431)
01-04 11:37:32.639: E/AndroidRuntime(2056): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.widget.TabHost.dispatchKeyEvent(TabHost.java:324)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1408)
01-04 11:37:32.639: E/AndroidRuntime(2056): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:2035)
01-04 11:37:32.639: E/AndroidRuntime(2056): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1505)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.app.Activity.dispatchKeyEvent(Activity.java:2426)
01-04 11:37:32.639: E/AndroidRuntime(2056): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1962)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3921)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3895)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3479)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3479)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3452)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3471)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3611)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3777)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2012)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1706)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1697)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1989)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.os.MessageQueue.nativePollOnce(Native Method)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.os.MessageQueue.next(MessageQueue.java:138)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.os.Looper.loop(Looper.java:123)
01-04 11:37:32.639: E/AndroidRuntime(2056): at android.app.ActivityThread.main(ActivityThread.java:5086)
01-04 11:37:32.639: E/AndroidRuntime(2056): at java.lang.reflect.Method.invokeNative(Native Method)
01-04 11:37:32.639: E/AndroidRuntime(2056): at java.lang.reflect.Method.invoke(Method.java:515)
01-04 11:37:32.639: E/AndroidRuntime(2056): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-04 11:37:32.639: E/AndroidRuntime(2056): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-04 11:37:32.639: E/AndroidRuntime(2056): at dalvik.system.NativeStart.main(Native Method)
当我再次转到标签2时也会发生这种情况。所以基本上在我进行第二次切换后,我无法返回。
任何人都知道如何解决这个问题?
问题解决了!这是我的最终代码:
public class QuestionsResultTabHost extends ActionBarActivity {
LocalActivityManager mLocalActivityManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_questions_results);
TabHost tabs = (TabHost) findViewById(R.id.tabHost);
mLocalActivityManager = new LocalActivityManager(this, false);
mLocalActivityManager.dispatchCreate(savedInstanceState);
tabs.setup(mLocalActivityManager);
Intent questionIntent = new Intent(this, QuestionActivity.class);
Intent currentIntent = getIntent();
Bundle extras = currentIntent.getExtras();
if (extras != null) {
questionIntent.putExtra("moduleId", extras.getString("moduleId"));
}
tabs.addTab(tabs.newTabSpec("questions").setIndicator("Fragen").setContent(questionIntent));
Intent resultIntent = new Intent(this, ResultActivity.class);
if (extras != null) {
resultIntent.putExtra("moduleId", extras.getString("moduleId"));
}
tabs.addTab(tabs.newTabSpec("result").setIndicator("Ergebnis").setContent(resultIntent));
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
protected void onResume ()
{
mLocalActivityManager.dispatchResume();
super.onResume ();
}
@Override
protected void onPause ()
{
mLocalActivityManager.dispatchPause(isFinishing());
super.onPause ();
}
@Override
protected void onStop ()
{
mLocalActivityManager.dispatchStop ();
super.onStop ();
}
@Override
protected void onSaveInstanceState (Bundle outState)
{
mLocalActivityManager.saveInstanceState ();
}
}
答案 0 :(得分:2)
@Override
protected void onResume ()
{
mLocalActivityManager.dispatchResume();
super.onResume ();
}
@Override
protected void onPause ()
{
mLocalActivityManager.dispatchPause(isFinishing());
super.onPause ();
}
@Override
protected void onStop ()
{
mLocalActivityManager.dispatchStop ();
super.onStop ();
}
@Override
protected void onSaveInstanceState (Bundle outState)
{
mLocalActivityManager.saveInstanceState ();
}
我将此添加到我的tabHost活动中,它完成了工作。