How do I retrieve the previous fragment upon opening an app from background? (app that wasn't killed)

两盒软妹~` 提交于 2019-12-02 03:02:17

I figured it out. here's the solution that works for me :

 fragmentManager = getFragmentManager();
        //check if on orientation change.. do not re-add fragments!
        if(mSavedInstanceState == null) {
            //instantiate the fragment manager
            fragmentTransaction = fragmentManager.beginTransaction();
            Fragment prevFragment = fragmentManager.findFragmentByTag(FragmentB.TAG);
            if (prevFragment != null) {
                fragmentTransaction.remove(prevFragment);
            }
            try {
                fragmentManager.popBackStack();
            }catch (IllegalStateException e){
                e.printStackTrace();
            }
            fragmentTransaction.commitAllowingStateLoss();

I was successfully able to popback fragmentB when my app was in background and hence reach fragmentA, so when I went back to my app, I was able to see fragmentA with updated values passed back from fragmentB.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!