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

邮差的信 提交于 2019-12-13 01:42:53

问题


I have a pretty unique issue where I am trying to retrieve previous fragment that was there before I went to the homescreen via the current fragment( last visible fragment)

Not to confuse any further, here's the steps to repro my issue

1 ) launch the mainactivity, from there open fragment A via a button click (say button A)

2) from fragment A , press another button ,say B, which will open fragment B.

3) Fragment B has a close and save button , pressing which leads me back to the previous backstack fragment A ( activity.backpressed())

4) Now, I have a scenario where I am calling fragment B, by pressing button B. After clicking on save and close, if I immediately press home button and go to the background and return back to the app, it restarts the the app, launches mainactivity all over, instead of opening fragment A ( which was the desired fragment).

Here's my code so far: This is my generic code for opening fragments:

 public void openFragment(@NonNull Fragment fragment, String tag) {
        Fragment f = getSupportFragmentManager().findFragmentByTag(tag);
        if (f != null) return;
        getSupportFragmentManager()
                .beginTransaction()
                .setCustomAnimations(R.anim.slide_up, R.anim.slide_bottom, R.anim.slide_up, R.anim.slide_bottom)
                .add(R.id.cl_root_view, fragment, tag)
                .addToBackStack(tag)
                .commit();
    }

On clicking button B, I call it as follows:

openFragment( FragmentB.newinstance(), fragmentB.TAG);

Once fragment B opens, I update my data and click save and close:

@OnClick(R.id.saveAndClose)
    void onSaveClick() {
        // update the contents 
          closeFragment();

    }

    protected void closeFragment() {
        Activity activity = getActivity();
        hideKeyboard();
        if (activity != null) {
            activity.onBackPressed();
        }
    }

As you can see the issue is, the data saves as expected, but if I click home button, right before the saving process completes and the fragment is popped (via backpressed), upon re-opening the screen /or resuming, instead of seeing fragmentA ( which was the underlying calling fragment) ,the app restarts. (I know the issue is happening since the activity is the same for all these fragments and calling activity.onBackPressed(); seems like everything in the stack is destroyed and upon resuming the app, seems like it calls the activity once again instead of the last fragment in the backstack). Any idea how to go about this?

I tried different solutions for similar issues online, but nothing works, I couldnt find any issue similar to mine. Also on savedinstancestate and retain instance state don't seem to do the trick. any ideas or suggesstions will be really helpful! thanks!


回答1:


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.



来源:https://stackoverflow.com/questions/56742838/how-do-i-retrieve-the-previous-fragment-upon-opening-an-app-from-background-ap

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