Weird Fragment lifecycle when Jetpacks Navigation component is used - android

So I've an Activity in which I set my navigation graph and NavigationView. So each menu item is a Fragment. Problem lies in the issue, that if I open menu item A, then open navigation drawer and choose menu item A again, it recreates the view - so far normal, but when I log out the lifecycle of the fragment, it works like this:
onCreate(), ... , [choose same menu item again], onCreate(), onDestroy(), onDetach()
Why does onDestroy and onDetach are the last called lifecycle methods? It would make sense if the fragment would be removed, but it is visible to user, so onDestroy should happen after user chooses menu item second time and should end with onCreate method. Any idea what is causing this?

Related

What will be fragment behavior when came from background. Considering fragment is in stack

I have a Navigation panel activity. With 5 fragments (Will name it as Fragment1, Fragment2, ...) in menu sections.
Now by Default, activity will display Fragment1.
If user navigate to Fragmentxtz from Fragment1. We will add the fragment on top of Fragment1.
Now user goes to background by pressing home button and open the app from tasks.
Now i know Fragmentxtz onStart will be called. But i see that Fragment1 onStart is also called.
Is this expected behavior ?
As you can see on Android Developers, your fragment will be called at onViewCreated().
https://developer.android.com/guide/components/fragments#Creating
The View has to be updated in case you changed the system language or something like that.
Providing a bit more context to what might be happening here.
If both of your fragments are added e.g. via FragmentTransaction.add() both of them will have onCreateView() called when the layout is restored for the user as Robert pointed out. From the system's point of view all of those fragments are relevant to the user and would be shown simultaneously.
If on the other hand you add fragments via FragmentTransaction.replace() only the topmost fragment on the back stack will receive the onCreateView() call. This can also be achieved by doing an add and remove of the old fragment. If you make this transaction reversible by the back stack after pressing the back button your previous fragment would receive the appropriate lifecycle callbacks.

Why is onDestroyView in a fragment called immediately after navigating with JetPack Navigation

Is it normal behaviour that onDestroyView() is called immediately when you navigate to a different fragment?
I call the following method to navigate:
findNavController().navigate(R.id.action_homefragment_to_detailsfragment)
Is there a way to avoid this? Or should everything be restored from a ViewModel? For example my last scroll position in a ScrollView..
Fragments on the back stack have their views destroyed, but the view state is saved and automatically restored when it comes to the top of the stack again (i.e., you hit the system back button).
Only views with an android:id have their state saved and restored, so make sure any important views have an id.

Fragments onOptionsItemSelected

How can i make the fragments on the backstack not to trigger the onOptionsItemSelected(). Because every time i create a new fragment it always trigger the onOptionsItemSelected() if i select an item in menu. Also this fragment will be reused throughout the activity so "return true" on onOptionsItemSelected() is not an option because the fragment that was created still the same with the backstack fragment.
It appears that onOptionsItemSelected will always be called in every new fragment that is added in activity Menu documentation , so to solve my problem is that I added a validation which appears this one
<CurrentFragment>this.equals(fragmentManager.findFragmentById(R.id.frame_container) to validate if the fragment displayed/ attached in activity is the fragment that trigger the onOptionsItemSelected.
Hope this will help to anyone who will have this kind of behaviour on fragments.

Fragment's Actionbar doesn't work when came back from another Activity sometimes

I have an Activity on which I am stacking two Fragments one at a time, when the second Fragment is visible and on click of some View I am launching another Activity and when I press back from there it will show the second Fragment but it doesn't update the ActionBar instead it will show the ActionBar of first Fragment.
Also, I am Overriding onResume() in the First Activity in which I am calling onResume() of visible Fragment. I am updating the ActionBar inside onResume() of the Fragment. Everything is getting called properly but still not updating the ActionBar.

Remove actionbar menu item when closing fragment

I'm using the code from the Example section of this page http://developer.android.com/guide/components/fragments.html
In landscape mode, the activity displays two fragments side-by-side, and on rotation, it launches a new activity containing the "detail" fragment.
The problem is that if I add a menu item to the actionbar from the detail fragment then rotate the screen to portrait to launch the detail fragment in a new activity then exit the activity, the menu item is still displayed even though the fragment supplying the menu item has been removed.
I've tried removing the detail fragment with a FragmentTransaction in onResume and then calling invalidateOptionsMenu(), but it doesn't remove the menu item.
I'm using ActionBarSherlock, and I've also tried supportInvalidateOptionsMenu()
Has anyone else run into this problem?
I thought I was removing the fragment, but I wasn't. After removing the fragment in onResume, my problem was solved.

Categories

Resources