1

I have two fragments FRAGMENT A and FRAGMENT B. WhenI go from FRAGMENT A to FRAGMENT B I am adding FRAGMENT A in backstack using addtobackstack(null) and also using replace() method. The problem is when I call popbackstack() from FRAGMENT B --> FRAGMENT A then OnCreateView() and onViewCreated() calls again and It loads the recycler view again in FRAGMENT A. I know all the instance are not loaded again but the problem is I do not want to load view again. Ex. in activity when i call finish previous activity remain same as we left.

NOTE : I don't want to use add() method in beginTransaction because it overlap other fragment.

Thanks

113408
  • 3,364
  • 6
  • 27
  • 54
Rohit Singh
  • 403
  • 4
  • 9
  • f you are adding the Fragments to the android.support.v4.app.FragmentManager you also have to call popBackStack() on the same FragmentManager. This code should solve the problem: if (getSupportFragmentManager().getBackStackEntryCount() > 0){ boolean done = getSupportFragmentManager().popBackStackImmediate(); } – Akash Pal Feb 16 '19 at 12:23
  • post your code here – Kishore Jethava Feb 16 '19 at 13:10
  • I think there is no need of code. I just need some help like how other apps ex. facebook, youtube manage there fragments when we change fragment with each other. – Rohit Singh Feb 18 '19 at 04:58

2 Answers2

2

For reference:

One more importance difference between add and replace is: replace removes the existing fragment and adds a new fragment. This means when you press back button the fragment that got replaced will be created with its onCreateView being invoked. Whereas add retains the existing fragments and adds a new fragment that means existing fragment will be active and they wont be in 'paused' state hence when a back button is pressed onCreateView is not called for the existing fragment(the fragment which was there before new fragment was added). In terms of fragment's life cycle events onPause, onResume, onCreateView and other life cycle events will be invoked in case of replace but they wont be invoked in case of add.

In Short for your use case, you need to use add and if you don't want the fragment views to overlap then you might replace the Fragment B view background color with a solid color instead of transparent.

Also don't forget to add android:clickable="true" to the parent view of your second fragment so it catches the clicks and they don't get propagated to the fragment below. Something like this:

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusable="true">
113408
  • 3,364
  • 6
  • 27
  • 54
  • is it good practice to use add because backstack will take more memory and how other apps like youtube managing their home page and other pages like if you see the home page is loaded at once next time when you come from another fragment they will not load data again from api. Even they also manage nested fragment in a same way. – Rohit Singh Feb 18 '19 at 04:55
  • So as i mentioned you can use add and give your fragment layout a solid background color so it will not overlap with the other fragment. If my answer was helpful, you can accept it and upvote so other benefit from it – 113408 Feb 18 '19 at 05:59
  • if i use solid background then onClick is performed in both fragments.(current and previous fragment). – Rohit Singh Feb 18 '19 at 06:02
  • don't forget to add `android:clickable="true"` on the main view of your second fragment. this will prevent clicks from getting propagated to the fragment below – 113408 Feb 18 '19 at 06:09
  • thanks its working but i have last question is there any alternate solution ? like how big apps are managing this condition because in most popular apps they don;t reload data again and again when a new fragment is added. – Rohit Singh Feb 18 '19 at 06:25
  • It depends on how you load your data. `Cache` is something that helps apps load faster but that's outside of the scope of this question. – 113408 Feb 18 '19 at 06:29
0

f you are adding the Fragments to the android.support.v4.app.FragmentManager you also have to call popBackStack() on the same FragmentManager.

This code should solve the problem:

if (getSupportFragmentManager().getBackStackEntryCount() > 0){
    boolean done = getSupportFragmentManager().popBackStackImmediate();
}
Akash Pal
  • 1,055
  • 8
  • 15
  • I think this is not what i am looking for. – Rohit Singh Feb 16 '19 at 12:31
  • so what are you trying to achiewve would you explain – Akash Pal Feb 16 '19 at 12:34
  • i don't want to call onViewCreate() again and again when i return from another fragment if it already loaded. I Know onDestroyView() is called when a new fragment is called but when i go back on click of button the previous fragment should not call api again and reload data. like finish() it does reload previous activity data from api if activity is already created. – Rohit Singh Feb 16 '19 at 12:41