I have three fragments, lets say Fragment A, B and C. Those are in a sliding tabs layout.
Illustration here : http://image.noelshack.com/fichiers/2015/27/1435684108-fdsf.png
When i open the application, i'm on fragment A and i do a volley request in it.
The list view on fragment A is populated by data that i get from volley response.
No problems so far.
When i switch from fragment A to fragment B, and then go back to fragment A, nothing change : the list view is still populated and fragment is not recreated ( normal situation ).
The problem is when i switch from fragment A to fragment C, and then go back to fragment A : It seems like fragment A is recreated ( i have my blank screen with the loading spinner until volley request gives a response and populates the list view, like when i launch the application )
I can switch by clicking on each tab name or by a sliding gesture, the problem is still the same.
So my question is : Why is fragment A recreated when i switch from A to C and then go back to A, but is not recreated when i switch from A to B and go back to A.
Thank you people and sorry for bad english.
Try to add this code
mCustomViewPager.setOffscreenPageLimit(3);
Which means the three fragment will be in the stack, and no need to recreate them.
Related
It's more like a conceptual question. Actually i have three fragments let's say A,B and C all are hosted in same activity. Fragment A and B are having List. In both fragments , on click of list item i am loading Fragment C. Now how to identify whether Fragment C is loaded from Fragment A or Fragment B ? I have to make changes in Fragment C's UI when it is loaded on click of any list item in Fragment A.
You would need to detect clicks in Fragment A and B. In their OnClickListeners, you could create two different setup methods for Fragment C.
on click of list item i am loading Fragment C
Since it's you who handles the click, simply add whatever you want to make other elements recognize the source of the click. Yet, I'd still say this is wrong approach - you should not really care what fragment it was literlay but rather, assuming fragments A, B reflect different data types, what data type it was. But still - you need to add that data to your bundle (or whatever you use to pass the data to fragment C)
Scenario: I have three fragments in ViewPager in activity.
Fragment One
FragmentTwo
FragmentThree
I noticed that switching from fragment one to three onPause() method is called
but when I switched from fragment one to two onPause is not called
Problem: I want to show some data in FragmentOne's TextView with a webservice on button click.I clicked that button and data is shown But I noticed when I switch from one to two and then come back to fragment one data is shown. I want to hide that data when I come back to one. Is there any callback . ?
You need to understand Viewpager first. viewpager creates fragment in advance before and keeps so that there is smooth flow when you switch, like if you are on Fragment 2. viewpager creates 1 and 3 and keeps.
and when you are on Fragment 3 it retains Fragment 1.
Anyways you can increase the limit of viewpager with this
viewPager.setOffscreenPageLimit(int num)
you can understand all the lifecylce of fragment in Viewpager by putting Logs in onPause and onResume in viewpager fragments.
Anyways coming back to your prob. you need to have custom interface like this . answer helped me too
Its happening because Fragment One is in backstack. Dont keep FragmentOne in backstack while switching to FragmentTwo and on Back press of FrgamentTwo call switchContent for FragmentOne again .
I have 2 fragments(Frag A, Frag B) which are shown in multi pane in landscape with different container id (R.id.containerA,R.id.containerB) respectively.
My screen flow for 1st fragment is FragA->FragA1->FragA2
My screen flow for 2nd fragment is FragB->FragB1
I am adding each fragment to backstack. So I have around 5 fragments in backstack.
Actual order of navigating is FragA->FragB->FragA1->FragB1->FragA2
Now when I press back button, I want FragB1 to be popped out first from backstack instead of FragA2. I know fragments are maintained in a stack but how to handle this particular multi pane scenario ?
Should I use reflection like mentioned in this post ?
Android Reorder Fragment Backstack
Any other alternatives ?
If you simply want to get FragB1 instead of FragA2, you can check by obtaining the name of the fragment and then go one step back if name of the fragment is FragA2. You can get the name using the following code:
FragmentManager.BackStackEntry backEntry=getFragmentManager().getBackStackEntryAt(getActivity().getFragmentManager().getBackStackEntryCount()-1);
String str=backEntry.getName();
I have a Fragment stack, which fragments are added to (not replaced). The problem is, because of memory issues, I don't want to have more than a specific amount of fragments in the stack. Suppose I have added fragments A, B, C, D to the stack. Now that I want to add the Fragment E, I want the A to be removed, using this piece of code:
fragmentManager.beginTransaction().remove(fragmentA).commit()
I get fragmentA using findFragmentByTag() and I am sure fragmentA exists and is in the stack. But this has no effect, and doesn't remove fragmentA.
What's wrong you think?
Is it possible to remove a fragment from bottom of the 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?