添加新项目后,操作栏菜单无法正确更新

时间:2014-09-21 16:14:51

标签: android android-actionbar

我最初有一个看起来像的菜单:

Line1
Line2
Line3

该应用已发布。然后我添加了另一个项目,所以它现在看起来像这样:

  Line1
  Line2
  Line4 << newly added item
  Line3

我更新了所有代码并且它有效,但有一些附加的副作用。原来的&#34; Line3&#34;正在拉起一个应用列表来分享一些东西&#39;。添加了第4行以打开另一个特定的应用程序。现在发生的事情是,如果我点击Line4,应用程序打开,一切顺利,直到我点击后退按钮返回到原始应用程序,我看到用于分享的应用程序列表&#39; Something&#39;打开了,这意味着我必须单击两次后退按钮。

这是我的代码(代表上面的第二个例子)

/**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);
       }
   }

这是我的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>

1 个答案:

答案 0 :(得分:1)

您忘记了return true;案例中的R.id.TheProblemStuff:行,因此它继续R.id.TheOtherProblemStuff案例,然后才停止。