Reputation: 330
I originally had a menu that looked like:
Line1
Line2
Line3
The app was published. I then added in another Item, so it now looks like this:
Line1
Line2
Line4 << newly added item
Line3
I updated all my code and it works, but with some added side effects. The original "Line3" was pulling up a app list to share 'something'. Line 4 was added in to open another specific app. What happens now is if I click on Line4, the app opens and all is well until I hit the back button to go back to the original app, I see that the app list for sharing 'Something' opened up as well, meaning I have to click the back button twice.
This is my code (representing the second example above)
/**creates action bar**/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
/**handles which action item is selected**/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.stuff:
startActivity(new Intent(android.provider.Settings.ACTION_LOCATION_SOURCE_SETTINGS));
return true;
case R.id.MoreStuff:
//things
return true;
case R.id.TheProblemStuff:
Intent intent1 = getPackageManager().getLaunchIntentForPackage("anApp");
if (intent1 != null)
{
/* we found the activity now start the activity */
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent1);
}
else
{
/* bring user to the market or let them choose an app? */
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("a website .com"));
startActivity(intent);
}
case R.id.TheOtherProblemStuff:
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.putExtra(Intent.EXTRA_SUBJECT, "Check out this app!");
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,("myapp"));
startActivity(Intent.createChooser(sharingIntent,"Share using"));//this is the menu that still comes up when "TheProblemStuff" is clicked. This use to be in the spot that "TheProblemStuff" was
return true;
default:
return super.onOptionsItemSelected(item);
}
}
and here is my xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.gpstest1.MainActivity" >
<item
android:id="@+id/Stuff"
android:title="@string/action_settings"
android:orderInCategory="1"
app:showAsAction="never"/>
<item
android:id="@+id/MoreStuff"
android:title="@string/action_map"
android:orderInCategory="2"
app:showAsAction="never"/>
<item
android:id="@+id/TheProblemStuff"
android:title="@string/speedometer"
android:orderInCategory="3"
app:showAsAction="never"/>
<item
android:id="@+id/TheOtherProblemStuff"
android:title="@string/action_share_app"
android:orderInCategory="4"
app:showAsAction="never"/>
</menu>
Upvotes: 0
Views: 46
Reputation: 3022
you forgot the return true;
line inside the R.id.TheProblemStuff:
case, so it continues to the R.id.TheOtherProblemStuff
case and only then stops.
Upvotes: 1