What is the flow, for example, for the options menu when we add them to the fragment and main action?

I am learning android and I am a little unsure how the menu options work. Here are the settings I have,

I have a main activity that has a snippet in it. The main activity of onCreateOptionsMenu looks like this:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

      

This menu_main.xml only has a settings button. I had to add refresh button in fragment so that I create new xml menu and add this code to fragment class

@Override
public void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.tempfragment, menu);
}

      

When I ran the app and did longpress I saw the refresh and settings button.

My question is, when we have menu options for the main activity and for its associated fragment, what is the flow? Does this connect both menus? I can see that this is a merge, but I'm a bit unsure about how it is handled internally? Can anyone explain this to me?

Edit Here is the menu file for the snippet

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

<item android:id="@+id/action_refresh" android:title="action_refresh"
    app:showAsAction="never"
    />
</menu>

      

+3


source to share


3 answers


If you have MainActivity

one that inflates a menu of options, menu_main.xml

then that menu of options will always be present on whatever snippet you create MainActivity

.

Within each piece of code, you can create a menu of options for that particular piece. Let's say you inflate menu_friends.xml

in FriendsFragment

. When an instance FriendsFragment

is created MainActivity

, both menu_main.xml

and will be shown menu_friends.xml

. You can repeat this for all fragments.



Edit: @Kartheek is wrong in his answer about calling setHasOptionsMenu(true)

in a snippet canceling the action menu. All this operator does is to create a menu with this snippet. The menu will be turned on. Look here: Calling setHasOptionsMenu (true) from a snippet results in multiple calls to onCreateOptionsMenu in action

+1


source


when do we have menu options for the main action and for its associated fragment?

If your fragment is called from setHasOptionsMenu

to true

, then it onCreateOptionsMenu

will be called in your fragment as well as in your activity. In fact, it combines menu items Activity

and Fragment

.

Does this connect both menus?

Yes. It combines both of them, which it displays as a whole.



Can someone explain this to me?

You can declare items for an options menu from your subclass Activity

or subclass Fragment

. If both your Activity and Fragment declare items for an options menu, they are merged into the UI. The activity elements appear first, followed by the elements of each chunk in the order in which each chunk is added to the activity. If necessary, you can reorder the menu items with an attribute android:orderInCategory

in each <item>

you need to move.

If your activity includes fragments, the system first calls onOptionsItemSelected()

for the activity, then for each fragment (in the order each fragment was added) until true is returned or all fragments are called.

+1


source


Don't give in to the main activity related to the fragment and the action bar is in action, so if you change the fragment, it doesn't feel like changing the activity ... Becuse from it, the action bar doesn't change, it just blends between the action bar you already have in action, and the action bar you have in the snippet.

Hope it helps you :)

+1


source







All Articles