从堆栈中弹出片段时,应用程序在后台崩溃
Application crashes in background, when popping a fragment from stack
应用程序崩溃,当我命中服务器 RPC 时,当 RPC 正在进行时,我将应用程序置于后台。同时,当 RPC 从服务器获得响应时,它从堆栈中弹出一个片段。弹出片段时,应用程序崩溃。
我读过有关创建 WeakReference 的内容,如果 activity 被销毁,它将为空。但不确定如何在这种情况下实施它。
以下是我的代码:
private void showFragment(SherlockFragment fragment) {
FragmentManager fm = getSupportFragmentManager();
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content, fragment);
ft.commit();
}
我在执行以下行时崩溃:
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
堆栈跟踪:
01-15 16:37:44.435: E/AndroidRuntime(28049): FATAL EXCEPTION: main
01-15 16:37:44.435: E/AndroidRuntime(28049): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
01-15 16:37:44.435: E/AndroidRuntime(28049): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1299)
01-15 16:37:44.435: E/AndroidRuntime(28049): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1310)
01-15 16:37:44.435: E/AndroidRuntime(28049): at android.support.v4.app.FragmentManagerImpl.popBackStack(FragmentManager.java:452)
01-15 16:37:44.435: E/AndroidRuntime(28049): at com.druva.inSync.ValidationActivity.run(ValidationActivity.java:93)
我遇到了完全相同的问题,我使用一个标志解决了这个问题。看起来有点"hacky",但它确实有效
public abstract class PopActivity extends Activity {
private boolean mVisible;
@Override
public void onResume() {
super.onResume();
mVisible = true;
}
@Override
protected void onPause() {
super.onPause();
mVisible = false;
}
private void popFragment() {
if (!mVisible) {
return;
}
FragmentManager fm = getSupportFragmentManager();
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
所以当你单独实现上面的代码时,当你恢复应用程序时,你会发现自己处于一个你真正想要弹出的片段中。您可以使用以下片段来解决此问题:
public abstract class PopFragment extends Fragment {
private static final String KEY_IS_POPPED = "KEY_IS_POPPED";
private boolean mPopped;
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putBoolean(KEY_IS_POPPED, mPopped);
super.onSaveInstanceState(outState);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mPopped = savedInstanceState.getBoolean(KEY_IS_POPPED);
}
}
@Override
public void onResume() {
super.onResume();
if (mPopped) {
popFragment();
}
}
protected void popFragment() {
mPopped = true;
// null check and interface check advised
((PopActivity) getActivity()).popFragment();
}
}
应用程序崩溃,当我命中服务器 RPC 时,当 RPC 正在进行时,我将应用程序置于后台。同时,当 RPC 从服务器获得响应时,它从堆栈中弹出一个片段。弹出片段时,应用程序崩溃。 我读过有关创建 WeakReference 的内容,如果 activity 被销毁,它将为空。但不确定如何在这种情况下实施它。
以下是我的代码:
private void showFragment(SherlockFragment fragment) {
FragmentManager fm = getSupportFragmentManager();
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.content, fragment);
ft.commit();
}
我在执行以下行时崩溃:
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
堆栈跟踪:
01-15 16:37:44.435: E/AndroidRuntime(28049): FATAL EXCEPTION: main
01-15 16:37:44.435: E/AndroidRuntime(28049): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
01-15 16:37:44.435: E/AndroidRuntime(28049): at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1299)
01-15 16:37:44.435: E/AndroidRuntime(28049): at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1310)
01-15 16:37:44.435: E/AndroidRuntime(28049): at android.support.v4.app.FragmentManagerImpl.popBackStack(FragmentManager.java:452)
01-15 16:37:44.435: E/AndroidRuntime(28049): at com.druva.inSync.ValidationActivity.run(ValidationActivity.java:93)
我遇到了完全相同的问题,我使用一个标志解决了这个问题。看起来有点"hacky",但它确实有效
public abstract class PopActivity extends Activity {
private boolean mVisible;
@Override
public void onResume() {
super.onResume();
mVisible = true;
}
@Override
protected void onPause() {
super.onPause();
mVisible = false;
}
private void popFragment() {
if (!mVisible) {
return;
}
FragmentManager fm = getSupportFragmentManager();
fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
}
}
所以当你单独实现上面的代码时,当你恢复应用程序时,你会发现自己处于一个你真正想要弹出的片段中。您可以使用以下片段来解决此问题:
public abstract class PopFragment extends Fragment {
private static final String KEY_IS_POPPED = "KEY_IS_POPPED";
private boolean mPopped;
@Override
public void onSaveInstanceState(Bundle outState) {
outState.putBoolean(KEY_IS_POPPED, mPopped);
super.onSaveInstanceState(outState);
}
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mPopped = savedInstanceState.getBoolean(KEY_IS_POPPED);
}
}
@Override
public void onResume() {
super.onResume();
if (mPopped) {
popFragment();
}
}
protected void popFragment() {
mPopped = true;
// null check and interface check advised
((PopActivity) getActivity()).popFragment();
}
}