每一个!
一年前,我开发了一个组合"活动组+标签小部件"的应用程序,它在android os 2.3 +上运行良好。但我在 os 4.0 上测试了它,我发现它有时会崩溃。- 如果我打开应用程序,我什么都不做,我点击"返回",它运行良好。- 如果我打开应用程序,在应用程序中导航(单击列表项,查看详细信息项然后返回(,然后当我返回到选项卡的"根"页面时,我单击"返回",它崩溃。(它应该正常关闭应用程序(--------在这种情况下,活动组启动新活动并接收后退键事件- 如果我打开应用程序,请像上面一样在应用程序中导航,但只是看到视图不是由活动组启动,而是由组中的根活动启动。Il 效果很好。
我的活动组中有一些代码
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发布的。
有什么想法吗?每个想法都将不胜感激!!
谢谢!
onBackPressed()
来实现所需的结果,您可以通过以下方式使用它。
public void onBackPressed(){
//Your code here
super.onBackPressed();
}
最重要的是,确保您已在将在活动组中使用的所有子活动中实施了此方法。
而不是这样做:
if (keyCode == KeyEvent.KEYCODE_BACK)
您可以尝试通过覆盖来处理后退按钮
public void onBackPressed() {}
并在其中编写代码。
你可以帮助这段代码:
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
super.onSaveInstanceState(outState);
}