android: application crash when i hit back button (activitygroup + TabWidget)

6k Views Asked by At

every one!

One year ago, I have developed an application with the combination "ActivityGroup + TabWidget", it works well on android os 2.3+. But I tested it on os 4.0, and I found it crash sometimes. - If I open application, I do nothing, and i hit "back", it works well. - If I open application, navigate in the application (click on list item, see detail item then go back), and then when I am back to the "root" page of a tab, i click "back", it crash. (It should close the application normally) --------in this case, the activitygroup start new activity and recieve the back keyevent - If I open application, navigate in the application like above, but juste see the views not started by activitygroup, but by the root activity in the group. Il works well.

There are some code in my 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);
}

And there's the error stack:

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)

I didn't use Fragment, because it wasn't published by android sdk when i developped this application.

Any ideas? Every idea will be appreciated!!
Thanks!

3

There are 3 best solutions below

3
On

Instead of doing like this:

if (keyCode == KeyEvent.KEYCODE_BACK)

you can try handling the back press button by overriding

public void onBackPressed() {}

and write your code inside it.

2
On

You can use onBackPressed() to achieve your desired results, you can use it in the following way.

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

Most importantly make sure you have implemented this method in all of your child activities which will be used in your activity group.

0
On

You can help this code:

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