I'm developing an Android application with a BottomBar which is consisted of 4 tabs which are all fragments, let's say A, B, C, D. While being at tab A, the user can press buttons which will open new fragments like A2, A3 etc. My problem is: after I reach A2, when I press on other tabs on the bottom bar (B,C,D), and then press back A, I turn back to fragment A instead of A2.
Is there any solution to save the current fragment (A2) when I switch back to tab A? Or shall I use seperate activities for each tab instead of fragments? I am using the replace() method of the FragmentTransaction class in order to switch between A and A2.
Related
I have an activity consisting of 3 fragments and a bottom navigation view which navigates the user to different fragments.
Let say the fragments are A, B and C.
The issue I am facing is that if I have performed some operation in A, and I navigate to B, then, when I again navigate to A (from bottom navigation view and NOT backpress) , the operations performed on A are lost, which I don't want.
For eg, in Youtube application, after searching something on Home fragment, if I navigate to "Subscriptions" or "Library", then if I renavigate to "Home", the search results are retained and also the state of the screen.
Kindly guide me how to achieve the same in my application
I have 3 fragments in my app: [a]->[b]->[c]. first fragment a is added, then it is replaced with b and then b is replaced with c. when the user is in fragment b he can go back to fragment a by pressing back button, but when in fragment c, fragments a and b must not be accessible with back button (pressing back button must close all fragments).
I have tried using
getSupportFragmentManager().popBackStackImmediate(name, FragmentManager.POP_BACK_STACK_INCLUSIVE);
but it pops all fragments from the top (current fragment) to the specified fragment which is not good idea. any simple solution?!
I'm developing an Android application with a BottomBar which is consisted of 4 tabs which are all fragments, let's say A, B, C, D. While being at tab A, the user can press buttons which will open new fragments like A2, A3 etc. My problem is: after I reach A2, when I press on other tabs on the bottom bar (B,C,D), and then press back A, I turn back to fragment A instead of A2.
Is there any solution to save the current fragment (A2) when I switch back to tab A? Or shall I use seperate activities for each tab instead of fragments? I am using the replace() method of the FragmentTransaction class in order to switch between A and A2.
I'd try implementing your A, B, C, D fragments in a ViewPager, then override setUserVisibleHint() on fragment A, which is called by the ViewPager when visibility changes, to save/restore the state according to the visibility arg that's passed in there.
This was asked in one of the interviews. The scenario is described as follows :
MainActivity (initially has Fragment A added to it.). On pressing a button, it goes to Fragment B, without adding A to backstack. On pressing a button in Fragment B, it adds 'B' to back stack, and goes to Fragment C. Now, in fragment C, I have a button through which I directly want to navigate to fragment A (which wasn't added to back stack). Is there any way possible to do this?
im going to write a little android app. Nothing complicated, with 6 "screens". 4 of them are on the same view navigation hierarchy level. 2 are subscreens of one of those 4.
For example screen A displays a list with information. By clicking on a list item the app will show screen A1. Screen A1 displays details about the previously selected list item. So Screen A1 is a subscreen of screen A. By clicking on the back button the app shows screen A.
Screen B has also a subscreen, called B1. The other screens are C and D which are on the same view navigation hierarchy as A and B (but not A1 and B1). So A, B, C, and D are on the top of the view navigation hierarchy. The App will start with displaying screen A. In the action bar (or in a sliding menu) you will find buttons to jump to screen B, C and D.
I hope you got an idea of what im trying to do. So normally I would say, every screen is a own activity. Im a big fan of fragments and i normally use them everywhere. In fact I caught myself implementing activities that contains only one fragment, that contains the main view layout. And I can say there is absolutely nothing wrong to do so (Fragments bring big advantages ... )
But now I wonder if it would be a good idea to use a single main activity and change only the fragments in this activity. So screen A, B, C, D, A1, B1 are Fragments instead of activities. So by navigating from Screen A to C I would simply replace fragment A with fragment C in the main activity (instead of starting a new Activity that contains the fragment C). The same way I would implement a navigation from screen A to subscreen A1.
So far I can't see nothing wrong with this approach. In my opinion it would be something like a ViewPager, just without swipe gestures to navigate between screens.
The only thing Im not sure about it (and thats my question) is the memory management. Whenever I do a replace fragment transaction (fragment manager) to navigate from screen A to B the fragment A should be destroyed and the memory should be garbage collected (somewhere in the future), right? When do I instantiate Fragment B? When the user clicks on the button to navigate to B (and repalce A)? Could this bring performance issues (time to instantiate the fragment)?
What if I set all fragments A, B, C, D, A1 and B1 to setRetainInstanceState(true)? Than the class member fields of each screen will be hold in the memory until the MainActivity is finished right?
Does anyone of you have experience with that?
A ViewPage uses some kind of "preloading" fragments and loads only the view layout of the next and previous fragment (setOffscreenPageLimit() )
Do you guys think I have to do something similar to avoid memory and performace issues?