Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - How to access actionbar's menu items in fragment class

Tags:

android

How should I access the actionbar's menu items in fragment ? I have tried this but nothing happened

@Override public boolean onOptionsItemSelected(MenuItem item) {     // TODO Auto-generated method stub      switch (item.getItemId()) {     case R.id.menu_refresh:         Toast.makeText(getActivity().getApplicationContext(), "clicked", Toast.LENGTH_SHORT).show();         return true;     default:         return super.onOptionsItemSelected(item);     } }  @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {     // TODO Auto-generated method stub     super.onCreateOptionsMenu(menu, inflater); } 
like image 445
Dao Quoc Khanh Avatar asked Jul 02 '14 08:07

Dao Quoc Khanh


People also ask

How do I access the activity toolbar in fragment?

if you are using custom toolbar or ActionBar and you want to get reference of your toolbar/action bar from Fragments then you need to first get instance of your Main Activity from Fragment's onCreateView Method like below. ImageView vRightBtn = activity. toolbar.

How do we hide the menu on the toolbar in one fragment in android?

When you click the show button to open a fragment, you can see the fragment menu items ordered before activity menu items. This is because of the menu item's android:orderInCategory attribute value. When you click the hide button to hide the fragment. The fragment menu items disappear from the action bar also.

What is onCreateOptionsMenu in android?

In this method, you can inflate your menu resource (defined in XML) into the Menu provided in the callback. For example: @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.game_menu, menu); return true; }


2 Answers

Follow this steps:

  • Add setHasOptionsMenu(true) method in your Fragment.

  • Override onCreateOptionsMenu(Menu menu, MenuInflater inflater) and onOptionsItemSelected(MenuItem item) methods in your Fragment.

  • Inside your onOptionsItemSelected(MenuItem item) Activity's method, make sure you return false when the menu item action would be implemented in onOptionsItemSelected(MenuItem item) Fragment's method.

Example:

Activity

@Override public boolean onCreateOptionsMenu(Menu menu) {     MenuInflater inflater = getSupportMenuInflater();     inflater.inflate(R.menu.main, menu);     return true; }  @Override public boolean onOptionsItemSelected(MenuItem item) {     switch (item.getItemId()) {     case R.id.activity_menu_item:         // Do Activity menu item stuff here         return true;     case R.id.fragment_menu_item:         // Not implemented here         return false;     default:         break;     }      return false; } 

Fragment

public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setHasOptionsMenu(true);     .... }  @Override public void onCreateOptionsMenu(Menu menu,MenuInflater inflater) {     // Do something that differs the Activity's menu here     super.onCreateOptionsMenu(menu, inflater); }  @Override public boolean onOptionsItemSelected(MenuItem item) {     switch (item.getItemId()) {     case R.id.activity_menu_item:         // Not implemented here         return false;     case R.id.fragment_menu_item:         // Do Fragment menu item stuff here         return true;     default:         break;     }      return false; } 
like image 69
joselufo Avatar answered Oct 04 '22 13:10

joselufo


In your fragments onCreate method add setHasOptionsMenu(true);.

If your fragment is in a ViewPager then the fragment with the ViewPager also needs the above line.

like image 24
Simon Avatar answered Oct 04 '22 11:10

Simon