我知道有几个类似的问题。
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, new LoginFragment()).commit();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Log.i(TAG, "DZActivity onSaveInstanceState");
}
@Override
public void onRequestStarted() {
DZActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i(TAG, "DZActivity onRequestStarted");
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ProgressFragment()).commit();
}
});
}
@Override
public void onSignIn(String accessToken) {
DZActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
Log.i(TAG, "DZActivity onSignIn");
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
}
});
}
按后退键后,我第二次启动我的应用程序,按“登录”键后,我得到:
08-07 18:02:54.609: I/DZActivity(3302): DZActivity onCreate
08-07 18:02:54.659: I/DZActivity(3302): DZActivity onResume
08-07 18:03:05.349: I/DZActivity(3302): DZActivity onRequestStarted
08-07 18:03:07.929: I/DZActivity(3302): DZActivity onSignIn
08-07 18:03:19.489: I/DZActivity(3302): DZActivity onPause
08-07 18:03:19.769: I/DZActivity(3302): DZActivity onStop
08-07 18:03:19.769: I/DZActivity(3302): DZActivity onDestroy
08-07 18:03:42.799: I/DZActivity(3302): DZActivity onCreate
08-07 18:03:42.819: I/DZActivity(3302): DZActivity onResume
08-07 18:03:49.559: I/DZActivity(3302): DZActivity onRequestStarted
08-07 18:03:49.559: E/AndroidRuntime(3302): java.lang.IllegalStateException: Activity has been destroyed
08-07 18:03:49.559: E/AndroidRuntime(3302): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1376)
08-07 18:03:49.559: E/AndroidRuntime(3302): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
08-07 18:03:49.559: E/AndroidRuntime(3302): at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:578)
如果我的活动被重新创建,甚至没有调用onSaveInstanceState,为什么要获取它?
最佳答案
这是已知问题,请看here
这是嵌套片段中的错误。基本上,当子碎片管理器从活动中分离出来时,它的内部状态会中断。
短期的解决方法是在片段中添加以下代码。
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
关于android - java.lang.IllegalStateException: Activity 已被破坏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25185950/