Reputation: 129
I want to only show my menu in my main activity then have a back button from my other activities instead of a menu. Right now I am just wondering how would I would remove the menu from the action bar on the activities I don't want it on.
My Manifest:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Insulter"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="Favourites"
android:launchMode = "singleInstance">
</activity>
<activity
android:name="Settings"
android:launchMode = "singleInstance">
</activity>
</application>
My menu opener inside my main activity:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent startSettings = (new Intent(Insulter.this,Settings.class));
startActivity(startSettings);
return true;
} else if (id == R.id.exit_the_app) {
finish();
return true;
} else if (id == R.id.favourites) {
Intent startFavs = (new Intent(Insulter.this, Favourites.class));
String[] objects = new String[favs.size()];
favs.toArray(objects);
final ArrayList<String> list = new ArrayList<>(Arrays.asList(objects));
startFavs.putStringArrayListExtra("favs",list);
startActivity(startFavs);
return true;
}
return super.onOptionsItemSelected(item);
}
Upvotes: 2
Views: 2118
Reputation: 3599
If you want menu in any of your activity, then you need to override onCreateOptionsMenu and onOptionsItemSelected... If you don't want menu, simply don't override these methods in your activity...
Upvotes: 2
Reputation: 61
You need to use following steps to remove options menu and add back button in your non-main activities --
Override onOptionsItemSelected method in following way--
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home:
finish();
return super.onOptionsItemSelected(item);
}
}
Upvotes: 0
Reputation: 784
For each activity that you create, there must be a menu file associated with it in the res/menu
folder. Simply delete or comment the corresponding <item>
tags. You may experiment with them by looking at the output show in the preview screen.
Note: I think this works only when an activity is created automatically using the UI (i.e not manually) since the corresponding menu file is generated automatically with it.
Upvotes: 0
Reputation: 39853
Activity.onCreateOptionsMenu is the place where it gets created. Just don't override this method or let it return false to not show the menu.
You must return true for the menu to be displayed; if you return false it will not be shown.
If you're using the same implementation for all your activities, define a field boolean isMain
and return it from onCreateOptionsMenu
public boolean onCreateOptionsMenu (Menu menu) {
if (!isMain)
return false;
[creating the menu here like before]
}
Upvotes: 2