The last version of WhatsApp does what I'm talking about. When you scroll between tabs the toolbar shows different menu options. I can implement ViewPager with adapter and fragments but not this. How could it be done? I really don't know what kind of trick is behind. It changes everytime you switch pages
In each Fragment you have to override onCreateOptionsMenu, with different menu/menu.xml files
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
Don't forget to call setHasOptionsMenu(true);, in onCreate of your Fragment
Related
I have an activity showing a few fragments.
Activity view contains only ViewPager initialized with custom FragmentPagerAdapter.
This adapter provide navigation among 3 fragments.
All seems to work fine except Action bar.
I override onCreateOptionsMenu() method in my fragments to create individual Action bar for any fragment:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
//fragment specific menu creation
}
When I do swipe, new fragment appears, but Action bar stay the same for a few seconds. Probably after a few seconds this method are called and Action bar get changed.
This looks pretty bad when action bar are changed in a while after swipe is finished.
How can I recreate action bar immediately, before swipe begin?
You can ask android to re-create the actionbar before it automatically does by calling invalidateOptionsMenu();
Do this somewhere close to the point where you change fragments to try and decrease the 'lag' between the fragment and actionbar changing.
Edit
a complete solution may look like this:
class activity extends Activity{
private void switchFragment(){
...do fragment changing stuff
activity.invalidateOptionsMenu();
}
}
class someFragment extends Fragment{
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
{
super.onCreateOptionsMenu(menu, inflater);
menu.clear();
//fragment specific menu creation
}
}
whichever fragment is open during the
activity.invalidateOptionsMenu();
will then call its
onCreateOptionsMenu
and you can do fragment specific menu creation in there
It can be done by implementing onCreateOptionsMenu and calling setHasOptionsMenu(true) in the onAttach callback in each fragment and change the actions accordingly.
To show the fragment without any actions setHasOptionsMenu(false) in it
If you are using ActionBar tabs, You would like to see this answer:
How can I change Action Bar actions dynamically?
Finally I couldn't find any sound way to achieve this.
The best would be to make actionbar a part of fragment, not activity, but it's impossible.
I end up with clearing actionbar menu and title when swipe begins(you can set special listener in PageView) and setting menu and title again when swipe complete and new fragment are shown.
In gives some time lag and actionbar looks strangely empty during swipe, but it's best you can do.
Android API is c...
Since android 4.2 now support NestedFragment , and added it to support v13.
I use this NestedFragment on a classic situation : Create fragmentA that can swipe left and right and consume a majority of the screen space, and insert fragmentB and fragmentC into each fragment page.
My Problem is the MenuItem I create in fragmentB and fragmentC can`t show on Activity`s actionbar.Which before I use NestedFragment , it works well.
got at some point the same problem. If you're using the ActionBarSherlock library this is a small bug. What you basically have to do is to call from your parent fragment from the onCreateOptionsMenu() method the onCreateOptionsMenu() method of the children, something like:
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
getChildFragment().onCreateOptionsMenu(menu, inflater);
}
hope this works, let me know.
Cheers.
I'm building an application with ActionBarSherlock that uses the Dropdown list navigation style. I have it set that each dropdown list item loads a different fragment, and that works fine. What doesn't work is the menu items in the actionbar. I have setHasOptionsMenu(true) in the fragments that I want to have menu items, as well as
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.fragment_menu, menu);
}
for the menus in the fragments. Every time I change fragments, I don't want the menu items appended which is what's happening. When one fragment is selected, the menu loads fine, then a different fragment is selected that isn't supposed to have menu items, and the menu items are the same as the previous fragment. Then if I go back to the first fragment, the menu items get doubled because they keep getting appended. How can I control this?
In the normal case the menu shouldn't be appended. What does your menu.xml look like? Do you have id's set? Maybe creating a menu in the Activity?
I figured it out. I wasn't using FragmentTransaction to load the fragments.
I have a layout with two fragments and both fragments have their own action bars, each of which have their own action items and menus. When my app is in landscape mode and both fragments are displayed on the screen, it looks like the framework is choosing to display the action bar on the "right" (or the 2nd fragment), which means the fragment on the left (1st fragment) is missing its action items and menu options.
Everything works fine when the app is in portrait mode, so I'm not sure if I should be doing something to handle the fragments when they are both displayed. Thanks.
EDIT
In each of my fragments I'm using this code to add menu items to the Action Bar:
In fragment 1:
#Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.fragment_menu_1, menu);
super.onCreateOptionsMenu(menu, inflater);
}
In fragment 2:
#Override
public void onCreateOptionsMenu(final Menu menu, final MenuInflater inflater) {
inflater.inflate(R.menu.fragment_menu_2, menu);
super.onCreateOptionsMenu(menu, inflater);
}
UPDATE:
Apparently using setRetainInstance(true) is what caused the menus not to refresh. I was using that because I have an AsyncTask that was throwing an exception if the device was rotated. So I fixed one issue, but broke another.
I think you're thinking about this incorrectly. The action bar is not displayed as part of any fragment, but actually as part of the activity. If you declare in your fragments that you provide action items via setHasOptionsMenu(true), then all will be displayed as part of the action bar. You can then take the appropriate action by overriding onOptionsItemSelected(MenuItem item).
I have an Activity containing a ViewFlipper and would like to show a different option menu for each view in that ViewFlipper. That is, the type of menu displayed when the menu button is pressed would depend on the type of the current view.
However, onCreateOptionsMenu() is called only once (when showing the option menu for the first time), so creating the different menus can't be implemented there.
How could I solve this?
First read about onPrepareOptionsMenu(Menu menu)
Each time the user presses the Menu on their Android device while inside one of your activities, the onPrepareOptionsMenu method is called. The first time the menu is shown (i.e. only once), the onCreateOptionsMenu method is called.
Basically, the onPrepareOptionsMenu method is where you should make any changes such as enabling/disabling certain menu items, or changing the menu item text depending on the circumstances.
So do this (Don't use onCreateOptionsMenu(Menu menu) )
//Dynamically create context Menu
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
menu.clear(); //Clear view of previous menu
MenuInflater inflater = getMenuInflater();
if(condition_true)
inflater.inflate(R.menu.menu_one, menu);
else
inflater.inflate(R.menu.menu_two, menu);
return super.onPrepareOptionsMenu(menu);
}