Answer the question
In order to leave comments, you need to log in
When running a fragment, is onCreateOptionsMenu() called twice?
There is a simple application built on fragments. When changing a fragment, a menu should be created using the ActionBar:
class BaseFragment extends Fragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle clicks
return true;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Create a menu
super.onCreateOptionsMenu(menu, inflater);
}
}
public void startFragment(BaseFragment fragment) {
getSupportFragmentManager()
.beginTransaction()
.replace(android.R.id.content, fragment)
.commit();
}
Answer the question
In order to leave comments, you need to log in
First you need to try this, I think it will help:
public void onCreate(Bundle savedInstanceState) {
setHasOptionsMenu(true);
super.onCreate(savedInstanceState);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question