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