Android ViewPager first and last pages fail on call onPause method - android

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

Related

Force onDestroyView on a fragment's view

I'm writing an app that loads user profiles and user's ratings for different places. The app uses fragments pervasively, and it's relatively easy to jump from a profile to a rated place.
As a user clicks a profile and gets to a rated place, they can click another profile on a rated place and go on and on.
The problem I'm having is memory related, when I'm looking at a ranked place and I click a profile, I switch from one Activity to the next. In both of these activities, after setContentView I load a fragment dynamically into layout space.
Now, as I shuttle between these activities, onSaveInstance state is almost always called, however since the Fragment displaying whatever was in the foreground before the activity switch, onDestroyView is not called.
What I would like is when onSaveInstanceState is called in these dynamic fragments, it to force onDestroyView to be called as well.
The only time onDestroyView seems to be called is when I add a Fragment to the back stack. When another activity comes to the foreground and this fragment is stopped, I'd like on DestroyView to be called as well.
The current workaround I want to implement is have an empty fragment with no view, and every time I call startActivity(Intent i), load this dummy fragment to destroy views and start the next activity. Then, when I come back, pop it off the back stack and restore the actual last fragment.
This however seems excessive. So, for a stopped fragment in a stopped activity with a new activity in front of it, how do I force it to destroy it's View?
First, you should not force or satisfy onDestroyView to fix your code, that's the job of the FragmentManager and the Android lifecyle # Pausing and Resuming an Activity. If you want to work with your existing code, use the other override methods onPause() or onStop().
Without posted code, I assume you're using the replace() method to display one fragment over another. This more or less forces you to manage the fragments yourself, some developers actually succeed in doing so with some struggle (look at other SO questions).
So my suggestion for you is either:
Maintain your own states, and show the proper fragments based on the state.
Use the BackStack and let the Fragment management handle the stack/states.

OnCreateView() not called on click of "Up" button

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.

How to call a method on an Android Fragment to start an activity?

I have an Android application that's using a SectionsPagerAdapter to create 3 separate fragments. What I would like one of the fragments to do is to automatically call a method (which launches an activity) whenever the user navigates to it from a tab bar. For example, right now I'm using the on resume() method to launch the activity. I also have a button set up to do it.
However, when I trace it through the debugger, the onResume() is only called when the fragment is first created. I've tried setting the fragment to null and recreating it from the SectionsPagerAdapter, but still none of the lifecycle methods are called. I've also set up a separate method in the fragment to launch the activity and calling it from the SectionsPagerAdapter before returning the adapter, but that doesn't launch it either - it just shows the display.
As I mentioned, the display has a button which allows the user to launch the activity manually, but that means the user has to press a button twice - once to get to the page, and once to launch the activity.
Is there a solution to this problem?
Call startActivity() at onTabSelected() method.

onAttach not called when navigating back through fragment backstack?

before commiting FragmentTransactions I call addToBackStack("") to implement back navigation properly.
In every fragment's onAttach() I call a function which updates my action bar. This works properly when moving forward, but not when moving back using the back button. I guess onAttach() is not being called.
Any solution for this? Alternatively which function is called each time so that I can update the UI in that function?

onLoadFinished not called on offscreen fragment until going back

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

Categories

Resources