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

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.

Related

Android going back to previous activity instead of deleting fragment?

I have 2 activities with fragments inside of them. Whenever I press the back button, the fragment gets destroyed (Used FragmentTransaction's replace method for adding them into the activity) instead of going back to the first activity immediately.
How can I achieve the behavior I want to have?
You can override onBackPressed method in your activity and check if the fragmentManager.getBackStackEntryCount()==0. If yes finish() the activity.
Hope it helps.

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?

Understanding the fragment and activity lifecycle and backwards navigation

I'm trying to understand some odd behavior. I have an ActivityA that calls a method in onCreate() to add FragmentA to R.id.fragment_container. Inside FragmentA I have a button that attaches FragmentB by using ActivityA's fragment manager (getActivity().getSupportFragmentManager()) and replacing the R.id.fragment_container and I also add it to the backstack. I also have another button that starts a new ActivityB.
When I navigate back from ActivityB I get: ActivityA onResume(), FragmentA onResume(). But when I navigate back from FragmentB I get: FragmentB onCreateView(), FragmentB onActivityCreated() then the 2 onResume().
So my questions is...why is the view state saved when a new activity is launched and not when the fragment is replaced and reattached. It looks much better to just restore that state rather than recreate the views and fetch that data again. This seems like opposite behavior from what I would expect so I'm clearly missing some fragment state saving/restoration step or something. It seems like the activity is just pausing FragmentA (and ActivityA) when ActivityB is launched and restoring it on back pressed but when FragmentB is attached FragmentA gets completely destroyed. I'm sure there's a way to prevent this I just can't seem to figure it out. Thoughts?
Just below your question are four tags android,android-fragments,android-lifecycle& android-navigation .Put your cursor over it for a while a black box will pop up . click on info tab and you will get best links to study that topics along with links to books .
Hope this will help you

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

Fragment onStart and onCreateActivity called after back button press

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

Categories

Resources