2

FragmentTransaction's hide() does not persist when Activity gets recreated upon config change(rotation, etc).

It re-adds all fragments in the back-stack, so the fragments previously hidden becomes visible. Ex) Fragments A(hide), B(show), C(hide), D(hide) are in back stack. When I rotate, for instance, it displays Fragment D on top after loading A, B, and C.

    FragmentTransaction ft = activity.getSupportFragmentManager().beginTransaction();

    ft.setCustomAnimations(R.anim.fade_in, R.anim.fade_out, R.anim.fade_in, R.anim.fade_out);

    // Hide all fragments other than the first
    for (int i = 1; i < fragments.length; i++) {
        if (fragments[i] != null) {
            ft.hide(fragments[i]);
        }
    }

    // Display only the first fragment
    if (fragments[0].isAdded()) {
        ft.show(fragments[0]);
    }
    else {
        ft.add(R.id.content_view, fragments[0], fragmentTag);
    }

    ft.commit();

Any workaround for this?

[Edit] Adding detail for the context of the problem.

What I'm trying to accomplish is to have bottom bar(my custom view that simply tells me which tab has been tapped) that I can switch between fragments to the same state it was at.

An example is Instagram, Quora, and Google Plus apps. enter image description here

[Edit 2 in response to Scrotos]

This is in my Presenter class. loadFirstScreen() performs a fragment transaction above. Others are just initializations like setting contents and registering listeners.

public void onCreate(Bundle savedInstanceState) {
    mView.initActionbar();
    mView.initViews();
    mModel.getUserProfile().subscribe(new Subscriber<UserProfile>() {
        @Override
        public void onCompleted() {
        }

        @Override
        public void onError(Throwable e) {
        }

        @Override
        public void onNext(UserProfile userProfile) {
            mView.initDrawer(userProfile);
        }
    });

    if (savedInstanceState == null) {
        mView.loadFirstScreen();
    }
}
awonderer
  • 665
  • 9
  • 26
  • Why are you hiding fragments and not adding/removing them? – JLamkin Oct 11 '16 at 00:47
  • I edited to give more context of the problem. When I add/remove/replace, the fragment would go through lifecycle like onCreateView to rebuild itself. I'm okay that it goes through the lifecycle methods, but I want the fragment to have the same view state as before; e.g. recycler view scroll position and such. – awonderer Oct 11 '16 at 00:55
  • You should be saving state of your fragments in onSaveInstanceState, including recyclerview scroll position and then setting the scroll position based on the bundle in onCreate or onCreateView – JLamkin Oct 11 '16 at 01:03
  • Are you ensuring that you are not re-adding fragments? Can I see your onCreate in the activity? Are you checking if the savedInstanceStateBundle exists and if so not re-creating the fragments? Are you adding the transactions to the back stack? – JLamkin Oct 11 '16 at 01:33
  • I do check for savedInstanceState's existence. If it exists, I don't do any fragment transaction. I'm editing the question to add my onCreate(). – awonderer Oct 11 '16 at 01:38
  • So are the fragments initially added in code or xml? – JLamkin Oct 11 '16 at 01:45
  • Not hardcoded in xml. All fragments are dynamically loaded as user taps on bottombar tabs by using add() as my code shows above(tried the route with replace() as well). My xml has a FrameLayout with id of content_view that I add fragment onto. – awonderer Oct 11 '16 at 01:49
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/125381/discussion-between-scrotos-and-awonderer). – JLamkin Oct 11 '16 at 02:00

2 Answers2

0

In the applications you show they are probably removing/adding fragments based on the container Id. While also maintaining the fragments state using onSaveInstanceState and the savedInstanceState bundle in onCreate or onCreateView

JLamkin
  • 721
  • 6
  • 17
0

The code in the question actually worked. One thing to note is to not set the actionbar title if hidden() is true in the fragments during recreation from config changes.

Thanks @Scrotos for chatting.

awonderer
  • 665
  • 9
  • 26