I'm trying to implement List->Detail scheme with details pagination.
I've single Activity for ListView and different Activity with ViewPager.
In FragmentStatePagerAdapter.getItem i instatniate new fragment for page and pass item id via setArguments Bundle.
After opening pager Activity two Fragment pages are created and onCreate, onCreateView, onActivityCreated being called.
BUT
onLoadFinished is called only for first Fragment (currently visible).
If i go back onLoadFinished is called for second Fragment.
Strage thing is that when i swipe to second (onLoadFinished for third not called), back to first and again go forward to second OnLoadFinished is called for third fragment and every next - after going back and forward always next fragment is being fully created in advance.
Is this bug or feature?
How fully loading can be forced?
We've solved this by manually calling onStart and onResume in onCreate.. Then onLoadFinished is called
Related
I have a viewpager that instantiate the same fragment several times, this is because each page represents a form and i just change its ID and TYPE, so this is handle well, but, I am trying to save the data at the OnPauseMethod, but, when I swipe the first page to second page, this method is not called. The same happends with the last page when I swipe back from this.
Any idea how to fix this?
OnPause is not called when navigate through fragments on ViewPager, the method is tied to the Activity's onPause method (will be called only when the Activity's onResume() or onPause() is called). May be you should do that onStop
I have a scenario where I have 2 fragments.
Clicking on a button in the first fragment takes you to the 2nd fragment.
By clicking the "UP" button in the 2nd fragment you'll get navigated back to the first fragment. Unfortunately the OnCreateView() method of the first fragment is not called.
Is there a way to call it? Which methods are called by clicking the "up" button?
It won't be called since 1st fragment is not yet detached from its activity and not destroyed yet. In your case, onResume() callback would be the best place to put your code.
OnCreateView doesnt get called because the fragment A is already created.
Read about the Fragmetn/Activity lifecycle and u will understand that.
OnResume will get called once pressed back from Fragment B, so u can put your logic in that method.
Imagine the following scenario:
Push Fragment A onto BackStack
Push Fragment B onto BackStack
Push Fragment C onto BackStack
Fragment B tries to make a async web request when its onResume is method is called.
Fragment C has a button called "Clear Backstack" that clears the backstack by calling popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE).
When PopBackStackImmediate is called it pops the Fragments off one by one until the stack is cleared. As each fragment is popped the fragment's onResume method is called. For Fragment B, I don't want the async web request to trigger since its going to be immediately destroyed/removed after its popped (because the entire backstack is being cleared).
In this case how can I detect if the entire backstack is being collapsed and skip the async web request on Fragment B in the OnResume method? Note: I'd still want the async web request to execute if Fragment B is popped/displayed by using the Back button.
Note: I'm using the latest compatibility/support library.
Option 1:
Have you determined what other lifecycle methods are being called - if it's only on resume, then move the async call further down in the lifecyle (onCreateView or onAttach for example) so that it's only called when moving through it in the normal manner.
Option 2:
When onResume is called you could do a getFragmentByTag on the fragment which has already been destroyed. If this is null you could then assume that the operation in action is the destruction.
This one seems pretty ugly to me.
Option 3:
Have C pass some flag up to the controlling activity (we'll call it Main), move the async call up, and when B wants to do the web request, have it call up to main to do so. If C has set the "I'm Destroying" flag, then don't perform the request.
Update: I was previously removing the old fragment and then adding the new fragment using FragmentTransaction.remove and FragmentTransaction.add respectively. Switching to FragmentTransaction.replace solved most of my problems when working with the backstack. See below:
The Android documentation has this to say about FragmentTransaction.replace:
Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.
I found the documentation to be slightly misleading because there is an important difference between replace vs. remove+add when the backstack is involved:
If the backstack A->B->C is built using remove+add, then popped back to fragment A, then fragment B's onResume method will be triggered.
If the backstack A->B->C is build using replace, then popped back to fragment A, then fragment B's onResume method will NOT be triggered.
Scenario:
I have a single Activity in which I have multiple fragments and I'm replacing one fragment with another using fragment transaction and add them to BackStack. I am doing JSON parsing and network related task on some fragments.
Problem:
My problem is that after replacing the fragment when I press back button to nevigate to last fragment the onStart and onActivityCreated methods called again. My code in these events execute each time I navigate to that fragment by using back button
But
Any value in EditText remain same in even after replacing the fragment and coming back to it using back button.
Why onStart and onActivityCreated executed each time?
Is there any method where I can put my code which do not execute after coming back to fragment?
UPDATE
Basically I want to set a button text once fragment is created. User can change that text. but when I return back to that fragment the users value change with the default text which I set on fragment creation time.
Thanks
You can put your code to run only one time at fragment creation in onCreate() of fragment..
onCreate()
The system calls this when creating the fragment. Within your implementation, you should initialize essential components of the fragment that you want to retain when the fragment is paused or stopped, then resumed.
See the life cycle of fragment.
You can also get more details here
I'm using a Fragment that loads an Image into a ImageView.
I want to change that image with startActivityForResult() but when I finish that Activity and go back to the Fragment,the Fragment reloads the first image again.
Probably because of the onStart() method.
How can I avoid the Fragment to reload after onActivityResult()?
I found out what the problem was.
The fragment loads in the information in the onStart() method.
I had a look at the fragment lifecycle and found out that I had to
load the information in the onActivityCreated() method.
This way the view is already created and I can refer to my xml-objects.
Check out this image that explains the fragment lifecycle:
http://developer.android.com/images/activity_fragment_lifecycle.png