I have a fragment where I can call ActivityCompat.invalidateOptionsMenu(getActivity()); and the method onCreateOptionsMenu() is successfully called on both: Activity and Fragment.
However, when I add a fragment on top of the fragment with:
transaction.add(R.id.fragment_home, fragment2, "fragment_ID");
transaction.addToBackStack(null);
transaction.commit();
and later close it (either by backPress or by getSupportFragmentManager().popBackStack();), ActivityCompat.invalidateOptionsMenu(getActivity()); becomes unresponsive. onCreateOptionsMenu() is not called at all.
I know for sure, that it will work if I use replace() instead of add(), however, for some reasons, I need to use add().
on Activity:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_home);
if (fragment != null)
fragment.onCreateOptionsMenu(menu, getMenuInflater());
return true;
}
on Fragment 1:
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
Log.d(TAG, "onCreateOptionsMenu() called with: ...");
// additional code
super.onCreateOptionsMenu(menu, inflater);
}
Please try below code :
#Override
public boolean onCreateOptionsMenu(Menu menu) {
Fragment fragment = getSupportFragmentManager().findFragmentById(R.id.fragment_home);
if (fragment != null) {
fragment.onCreateOptionsMenu(menu, getMenuInflater());
return true;
}
getMenuInflater().inflate(R.menu.menu_layout, menu);
return super.onCreateOptionsMenu(menu);
}
Related
I have a Main activity that hosts a navigation drawer and multiple fragments. Each fragment has its own set of menu items.
Now the problem starts when I add the onOptionsItemSelected method, the navigation Drawer won't open. And when I don't add the onOptionsItemSelected method, navigation Drawer opens but then menu items are not working.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_payment_type_data_stats, parent, false);
setHasOptionsMenu(true);
return v;
}
#Override
public void onCreateOptionsMenu(#NonNull Menu menu, MenuInflater inflater) {
menu.clear();
inflater.inflate(R.menu.menu_payment_type_stats, menu);
Objects.requireNonNull(((MainActivity) Objects.requireNonNull(getActivity())).getSupportActionBar()).setTitle("title");
super.onCreateOptionsMenu(menu, inflater);
}
#SuppressLint("NonConstantResourceId")
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_today:
getDataForToday();
break;
case R.id.action_past_seven:
getDataForPeriod(calendarDates.pastSeventhDate(), calendarDates.todayDate());
break;
case R.id.action_custom_date:
openDatePickerDialog(sdf);
break;
}
return true;
}
If you have any suggestions for fixing this problem, please share.
Can set manually -
`drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDrawer.openDrawer(GravityCompat.START);
}
});`
or can try this
mDrawerToggle.syncState();
MainActivity changes:
All you have to do is change onOptionsItemSelected in MainActivity from
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return true;
}
and change it to
#Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item); //important
}
As far as the onCreateOptionsMenu method in MainActivity, you can leave it empty with just
return true
Fragment changes
For fragment (each fragment can have separate action menu items) you need to implement 3 methods
onCreateView
onCreateOptionsMenu
onOptionsItemSelected
Example:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_payment_type_data_stats, parent, false);
setHasOptionsMenu(true); //important
return v;
}
#Override
public void onCreateOptionsMenu(#NonNull Menu menu, MenuInflater inflater) {
menu.clear();
//each frag can have different menu
inflater.inflate(R.menu.menu_payment_type_stats, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#SuppressLint("NonConstantResourceId")
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_today:
getDataForToday();
break;
case R.id.action_yesterday:
getDataForPeriod(calendarDates.yesterdayDate(), calendarDates.yesterdayDate());
break;
case R.id.action_past_seven:
getDataForPeriod(calendarDates.pastSeventhDate(), calendarDates.todayDate());
break;
case R.id.action_past_thirty:
getDataForPeriod(calendarDates.pastThirtiethDate(), calendarDates.todayDate());
break;
case R.id.action_custom_date:
openDatePickerDialog(sdf);
break;
}
return false;
}
My activity has a menu which includes a SearchView, and I'm setting the SearchView visibility programmatically. I'm using savedInstanceState to preserve the Visibility value eg between rotations, but this aspect is not working: in the scenario where the SearchView visibility is GONE before rotation, the SearchView icon is showing after rotation.
If I debug and evaluate mSearchView.getVisibility() after picking up its value from savedInstanceState, it appears to be correctly set to 8.
There are lines in my code which setVisibility(View.VISIBLE), but none of them are hit between the value from savedInstanceState being set, and the rotated layout appearing to the user.
Layout:
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- "Mark Favorite", should appear as action button if possible -->
<item android:id="#+id/action_search"
android:title="#string/action_search"
app:actionViewClass="androidx.appcompat.widget.SearchView"
app:showAsAction="ifRoom"/>
Activity (onSaveInstanceState):
public void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
switch (mSearchView.getVisibility()){
case View.VISIBLE:
outState.putInt("SearchViewVisibility",View.VISIBLE);
case View.GONE:
outState.putInt("SearchViewVisibility",View.GONE);
};
}
Activity (Menu setup):
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
//setup SearchView and callbacks
final MenuItem searchItem = menu.findItem(R.id.action_search);
mSearchView = (SearchView) searchItem.getActionView();
if(mSavedInstanceState != null){
switch (mSavedInstanceState.getInt("SearchViewVisibility")){
case View.VISIBLE:
mSearchView.setVisibility(View.VISIBLE);
case View.INVISIBLE:
mSearchView.setVisibility(View.INVISIBLE);
case View.GONE:
mSearchView.setVisibility(View.GONE);
}
}
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){
#Override
public boolean onQueryTextSubmit(String query) {
SetFragmentAndFilter(query);
return true;
}
#Override
public boolean onQueryTextChange(String query) {
// check whether this change is clearing all text from the search
if(query.isEmpty()){
// close the searchView
mSearchView.post(new Runnable(){
#Override
public void run(){
mSearchView.clearFocus();
}
});
mSearchView.setIconified(true);
}
//have the list match the new query text
SetFragmentAndFilter(query);
return true;
}
});
return true;
}
Would really appreciate any thoughts!
You should use setVisible method to set the visibility of a menu item.
Change your code to
// Add this property
MenuItem mSearchMenuItem;
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean("isVisible", mSearchMenuItem.isVisible());
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
//setup SearchView and callbacks
mSearchMenuItem = menu.findItem(R.id.action_search);
if (mSavedInstanceState != null) {
boolean isVisible = mSavedInstanceState.getBoolean("isVisible", true);
mSearchMenuItem.setVisible(isVisible);
}
mSearchView = (SearchView) mSearchMenuItem.getActionView();
mSearchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
#Override
public boolean onQueryTextSubmit(String query) {
return true;
}
#Override
public boolean onQueryTextChange(String query) {
// check whether this change is clearing all text from the search
if (query.isEmpty()) {
// close the searchView
mSearchView.post(new Runnable() {
#Override
public void run() {
mSearchView.clearFocus();
}
});
mSearchView.setIconified(true);
}
return true;
}
});
return true;
}
there are many questions about the topic but i can't figure out my problem. I have a menu declared in my MainActivity(ActionBarActivity). Now i want to work with MenuItem in onOptionsItemSelected of a Fragment class. Here is my MainActivity methods
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// toggle nav drawer on selecting action bar app icon/title
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle action bar actions click
switch (item.getItemId()) {
case R.id.add_note:
createNewNote();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void createNewNote() {
Intent addIntent = new Intent(MainActivity.this, AddNote.class);
startActivity(addIntent);
}
And Fragment methods
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add_note:
Toast.makeText(getActivity(), "Entered into fragment", Toast.LENGTH_LONG).show();
createNewNote();
return true;
default:
break;
}
return super.onOptionsItemSelected(item);
}
private void createNewNote() {
Intent addIntent = new Intent(getActivity(), AddNote.class);
startActivity(addIntent);
}
Here in MainActivity onOptionsItemSelected get called even in Fragment but doesn't in Fragment as i don't see Toast in Fragment. I think something is missed in my code. Thanks in advance.
inside onOptionsItemSelected() in activity inside your switch after calling createNewNote() instead of returning true return super.onOptionsItemSelected(item)
you must call setHasOptionsMenu() inside onCreate of fragment for menu related method to work.
Your onCreateOptionsMenu() method does not inflate the menu file:
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
}
I have a Fragment with menu:
public class FragmentA extends Fragment {
public FragmentA() {
setHasOptionsMenu(true);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.activity_main_actions, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
I would like to change menu but it doesn't work and keep the old action menu
Fragment B is equals like above with different inflate XML menu.
public class FragmentB extends Fragment {
public FragmentB() {
setHasOptionsMenu(true);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.**action_ranking**, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
EDITED:
Can be useful to use different menu layout for different fragments and 1 menu layout for main activity and differents id
Put setHasOptionsMenu(true) in constructor and inflate fragment specific menu.
public class FragmentA extends Fragment {
public FragmentA() {
setHasOptionsMenu(true);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.fragmenta_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
menu in main activity
public class MainActivity extends Activity {
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.main_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
}
Can all be done via Fragment - no need to inflate menu from activity:
public class UpdateFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
setHasOptionsMenu(true);
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.update_menu, menu);
super.onCreateOptionsMenu(menu, inflater);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id){
case R.id.navUpdateProfile:
showToast("navUpdateProfile");
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
If you have several fragments that share the same menu with some exceptions.
class BaseFragment:Fragment(){
open var menuId = R.menu.default_menu
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setHasOptionsMenu(true) // will apply to all children
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
inflater.inflate(menuId, menu) // will apply to all children except for overridden
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// all menu ids can be listed here unless specific to code in child
when (item.itemId) {
R.id.menu_option_1 -> {
// do something
}
R.id.menu_option_2 -> {
//do something
}
return false
}
}
class ChildFragment:BasFragment(){
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
menuId = R.menu.menu_2 // change to a different menu as desired here
}
}
I'm very new to Android development.
In my MainActivity.java file, I've declared an onOptionsItemSelected(MenuItem menu) method that allows the user to jump between the current MainActivity.java page and another page I created called Settings.java.
When the user goes to the Settings.java page, and they click on the Home option from the menu, nothing happens. I know this is because the onOptionsItemSelected(MenuItem menu) is only defined in the MainActivity.java class and not in the Settings.java class.
I'm overcoming this right now by copying the onOptionsItemSelected(MenuItem menu) from MainActivity.java into Settings.java. But this is very redundant.
Where should I be declaring methods that could be reused in different classes?
MainActivity.java
public class MainActivity extends Activity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
showMsg("Settings Clicked");
this.startActivity(new Intent(this, Settings.class));
return true;
case R.id.action_home:
showMsg("Home clicked");
this.startActivity(new Intent(this, MainActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void showMsg(String msg) {
Toast toast = Toast.makeText(this.getBaseContext(), msg, Toast.LENGTH_LONG);
toast.show();
}
}
Settings.java
public class Settings extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_settings:
this.startActivity(new Intent(this, Settings.class));
return true;
case R.id.action_home:
this.startActivity(new Intent(this, MainActivity.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
If you create an Activity called BaseActivity that has your common onOptionsItemSelected() code then extend this class to create MainActivity and Settings both of these classes will use the super class' (i.e. BaseActivity's) onOptionsItemSelected().
Another approach is to switch to Fragments, since Fragments use the host Activity's onOptionsItemSelected() as well as their own. Both of these tactics allow you to use "centralized" code and not have to maintain multiple "cut and paste" copies.