Fragments onOptionsItemSelected - android

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.

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.

Weird Fragment lifecycle when Jetpacks Navigation component is used

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?

How can I maintain UI state of my previous state in android?

Explanation:
I have two fragments and one activity. I have gone from one fragment to another fragment and my first fragment is visible from the activity.
In the first fragment onCreateView() method I called my API call. After call my data I set my data into the ListView. When I click on one of the rows of the ListView My second fragment becomes visible. Everything is working fine till now.
Problem
When I click on the back button of the device from the second fragment my previous fragment is visible but API call has been started.
How can I stop to refresh UI of the first fragment when I press back button?
For more detail See the Instagram Android Application.
If you are using addtobackstack() to maintain fragment state and call replace() method to call second fragment from first fragment in that case every time when you pressed back button from second fragment the onCreateView() method of first fragment will be called.
So the solution is call your API in onAttach() or onCreate() method.
Hope this will help you.

Navigate up to last shown fragment

I've implemented a NavigationDrawer within the MainActivity which containts two Fragment. The second Fragment contains a ListView that opens a new Activity (onItemClick) that shows the according detailed data. (I guess that's pretty much the master/detail flow).
When I use the up button to navigate back I got shown the first fragment and not the second fragment with the ListView.
Any ideas how to solve that problem?
Make method in MainActivity for example setFragment(int whichFragment);
and set fragment you want in it, you should already have code that do that and than call that method in onBackPressed() method.
For your question about another fragment, well it depends on how your master/detail flow is suppose to work, it is not a problem to use another activity if you dont need left menu any more, but if you do need left menu then use another fragment.
Best regards

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