Suppose I have different fragments within an activity in backstack such as [1]->[2]->[3]->[4]->[5] with Fragment 5 presently visible on the screen. Now I want to go to Fragment 2 without destroying Fragments 3 and 4 on a single press of a button. How can this be done?
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 a question. Refer to this:
How to manage multiple Activity stacks in an Android app, like you would on iOS with multiple UINavigationControllers within a UITabBarController?
With this visualization:
From the image above:
I have 4 tabs with button view
Tab 1 pressed, activity appears. Go to another sub activities, then tab 3 pressed.
When tab 3 pressed, activity appears, then tab 1 pressed should resume previous sub activities.
Then my problem is :
When tab 1 is pressed, i always start activity without the previous sub activities within it.
Then how can I resume activity contains sub activities that already in stack?
You should use fragments here inside tabs. Fragments stack are easy to manageable and lightweight process.
I have an app which contain three tab with three fragment mainly A,B,C lets suppose I am on tab 2 which contain fragment B when app goes in background and again resume then fragment A is showing instead of fragment B. Ho do I resolve that?
I have to implement navigation structure like iOS or you can say like browser. where in my application it is like i have 3 tabs and for each tab there is flow of multiple fragments like
Tab 1 --> Fragment 1--> Fragment 2--> Fragment 3
Tab 2 --> Fragment 1--> Fragment 2
Tab 3 --> Fragment 1--> Fragment 2--> Fragment 3--> Fragment 4
And navigation is like when user switches tab then can view last opened Fragment of that Tab and on pressing Back should go back to previously opened Fragment of that Tab. All these Fragments load from single Fragment Activity so what I'm thinking is to manage Navigation manually by creating three different stacks keeping track of opened Fragments in those Tabs.
What i want is a method by which i can bring previously added fragment to front without pop of any other even there are some which added after that fragment. And on back press I'll pop the current fragment and bring in front another previously added fragment according to my own managed stacks.
I searched for method to bring in front previously loaded fragment by tag but they will pop all the fragments loaded after that fragment which is not acceptable for me.
You can try
getSupportFragmentManager().beginTransaction().attach(fragmentA).commit;
or
getSupportFragmentManager().beginTransaction().show(fragmentA).commit;
and hide other fragments you have in your FragmentActivity by looping through all given by
getSupportFragmentManager().getFragments();
//This gives list of fragment in stack
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?