android:应用程序崩溃当我点击后退按钮(activitygroup + TabWidget)

时间:2012-05-18 19:30:10

标签: android tabbar back activitygroup

每个人!

一年前,我开发了一个使用“ActivityGroup + TabWidget”组合的应用程序,它在android os 2.3+上运行良好。但是我在os 4.0上测试了它,我发现它有时会崩溃。 - 如果我打开应用程序,我什么也不做,我点击“返回”,它运行良好。 - 如果我打开应用程序,在应用程序中导航(单击列表项,查看详细信息项然后返回),然后当我回到选项卡的“根”页面时,我单击“返回”,它会崩溃。 (它应该正常关闭应用程序)--------在这种情况下,activitygroup启动新活动并接收返回的keyevent - 如果我打开应用程序,请在上面的应用程序中导航,但是juste会查看不是由活动组启动的视图,而是通过组中的根活动。 Il运作良好。

我的ActivityGroup中有一些代码

public void startActivity(Intent intent) {
// Start the root activity withing the group and get its view
    View view = getLocalActivityManager().startActivity(id, intent)
                                        .getDecorView();
    // Replace the view of this ActivityGroup
    replaceView(view);
}

public void back() {
    if(history.size() > 1) {  //history = new ArrayList<View>(); global variable
        history.remove(history.size()-1);
        setContentView(history.get(history.size()-1));

        mIds.pop();
    }else {
        finish();    // I tried to commment it too, but no lucks
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        back();

        return true;
    }

    return super.onKeyDown(keyCode, event);
}

还有错误堆栈:

05-18 18:47:47.687: E/AndroidRuntime(6828): FATAL EXCEPTION: main
05-18 18:47:47.687: E/AndroidRuntime(6828): java.lang.IllegalStateException: Can not                 perform this action after onSaveInstanceState
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1213)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at     android.app.FragmentManagerImpl.popBackStackImmediate(FragmentManager.java:442)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.app.Activity.onBackPressed(Activity.java:2121)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.app.Activity.onKeyUp(Activity.java:2099)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.view.KeyEvent.dispatch(KeyEvent.java:2557)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.app.Activity.dispatchKeyEvent(Activity.java:2329)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1859)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1361)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.widget.TabHost.dispatchKeyEvent(TabHost.java:298)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1248)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1859)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1361)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.app.Activity.dispatchKeyEvent(Activity.java:2324)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1806)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3300)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.view.ViewRootImpl.handleFinishedEvent(ViewRootImpl.java:3273)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2436)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.os.Looper.loop(Looper.java:137)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at android.app.ActivityThread.main(ActivityThread.java:4340)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at java.lang.reflect.Method.invokeNative(Native Method)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at java.lang.reflect.Method.invoke(Method.java:511)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-18 18:47:47.687: E/AndroidRuntime(6828):     at dalvik.system.NativeStart.main(Native Method)

我没有使用Fragment,因为当我开发这个应用程序时它不是由android sdk发布的。

有什么想法吗?每个想法都将受到赞赏!!
谢谢!

3 个答案:

答案 0 :(得分:2)

您可以使用onBackPressed()来获得所需的结果,您可以通过以下方式使用它。

public void onBackPressed(){
  //Your code here
  super.onBackPressed();
}

最重要的是确保您已在所有将在您的活动组中使用的子活动中实施此方法。

答案 1 :(得分:0)

而不是这样:

if (keyCode == KeyEvent.KEYCODE_BACK)

您可以尝试通过覆盖

来处理后退按钮

public void onBackPressed() {}

并在其中编写代码。

答案 2 :(得分:0)

您可以提供以下代码帮助:

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
    super.onSaveInstanceState(outState);
}