应用程序崩溃的背景下,从弹出堆栈中的片段时, [英] Application crashes in background, when popping a fragment from stack

查看:213
本文介绍了应用程序崩溃的背景下,从弹出堆栈中的片段时,的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

应用程序崩溃,当我打一个服务器RPC,并在RPC过程中,我把应用程序在后台。同时,当在RPC获取从服务器的响应,它弹出从栈的片段。而弹出的片段,应用程序崩溃。
我已阅读有关创建了WeakReference ,如果​​这将是空活性被破坏。但不知道如何实现它这种情况下。

以下是我的code:

 私人无效showFragment(SherlockFragment片段){
    FragmentManager FM = getSupportFragmentManager();
    fm.popBackStack(NULL,FragmentManager.POP_BACK_STACK_INCLUSIVE);
    FragmentTransaction英尺= fm.beginTransaction();
    ft.replace(R.id.content,片段);
    ft.commit();
}

我得到一个崩溃上执行以下行:

fm.popBackStack(NULL,FragmentManager.POP_BACK_STACK_INCLUSIVE);

堆栈跟踪:

  16 01-15:37:44.435:E / AndroidRuntime(28049):致命异常:主要
01-15 16:37:44.435:E / AndroidRuntime(28049):java.lang.IllegalStateException:的onSaveInstanceState后无法执行此操作
01-15 16:37:44.435:E / AndroidRuntime(28049):在android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1299)
01-15 16:37:44.435:E / AndroidRuntime(28049):在android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1310)
01-15 16:37:44.435:E / AndroidRuntime(28049):在android.support.v4.app.FragmentManagerImpl.popBackStack(FragmentManager.java:452)
01-15 16:37:44.435:E / AndroidRuntime(28049):在com.druva.​​inSync.ValidationActivity $ 2.run(ValidationActivity.java:93)


解决方案

我有完全相同的问题,我用一个标志解决。这似乎有点哈克,但它的工作

 公共抽象类PopActivity延伸活动{    私人布尔mVisible;   @覆盖
    公共无效onResume(){
        super.onResume();
        mVisible = TRUE;
    }    @覆盖
    保护无效的onPause(){
        super.onPause();
        mVisible = FALSE;
    }    私人无效popFragment(){
        如果(!mVisible){
            返回;
        }        FragmentManager FM = getSupportFragmentManager();
        fm.popBackStack(NULL,FragmentManager.POP_BACK_STACK_INCLUSIVE);
    }
}

所以,当你执行上述code单独当您恢复应用程序,你会发现自己在你真的想被弹出一个片段。您可以使用下面的剪断来解决这个问题:

 公共抽象类PopFragment扩展片段{    私有静态最后弦乐KEY_IS_POPPED =KEY_IS_POPPED;
    私人布尔mPopped;    @覆盖
    公共无效的onSaveInstanceState(捆绑outState){
        outState.putBoolean(KEY_IS_POPPED,mPopped);
        super.onSaveInstanceState(outState);
    }    @覆盖
    公共无效的onCreate(@Nullable捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        如果(savedInstanceState!= NULL){
            mPopped = savedInstanceState.getBoolean(KEY_IS_POPPED);
        }
    }    @覆盖
    公共无效onResume(){
        super.onResume();
        如果(mPopped){
            popFragment();
        }
    }    保护无效popFragment(){
        mPopped = TRUE;
        //空检查和界面检查建议
        ((PopActivity)getActivity())popFragment();
    }
}

Application crashes, when I hit a server RPC, and when the RPC is in progress, I put the application in background. Meanwhile, when the RPC gets the response from server, it pops a fragment from stack. While popping the fragment, the application crashes. I have read about creating WeakReference, which will be null if the activity is destroyed. But not sure how to implement it this case.

Following is my code :

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();
}

I get a crash on executing the following line:

fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

StackTrace :

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$2.run(ValidationActivity.java:93)

解决方案

I had the exact same problem, which I resolved using a flag. It may seem a little "hacky", but it does the job

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);
    }
}

So when you implement the above code alone when you resume the app you will find yourself in a fragment that you actually want to be popped. You can use the following snipped to fix this issue:

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();
    }
}

这篇关于应用程序崩溃的背景下,从弹出堆栈中的片段时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆