Recognize a menu of options from a snippet? Android

I currently have an app with 4 pages, these pages are added by fragments using the pageViewer so I can swipe in between.

I want to be able to access the Options menu (which has a default “Settings” option) from each page, so I can execute commands specifically for one page, such as the 'refresh' element that refreshes data on the current page.

Can anyone point me in the right direction? Thank!

0


source to share


1 answer


Everyone Fragment

can declare their own menu, which will automatically merge with the current action menu.

class YourPage extends Fragment
{

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        // Signal that this fragment has proper actions
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) 
    {
        super.onCreateOptionsMenu(menu, inflater);

        // The menu will be added to the action bar
        inflater.inflate(R.menu.fragment_page_menu, menu);      
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        switch (item.getItemId())
        {
            case R.id.action_refresh:
            {
                // Handle the action...

                return true;
            }


            default:
                return super.onOptionsItemSelected(item);
        }       
    }   
}

      



Here is a complete example I did, along with a related tutorial .

0


source







All Articles