2014-09-21 78 views
0

後能夠正確地更新我本來看起來像一個菜單:操作欄菜單中未添加新項目

Line1 
Line2 
Line3 

該應用程序發佈。然後我在另一個項目增加,所以現在看起來是這樣的:

Line1 
    Line2 
    Line4 << newly added item 
    Line3 

我更新了我的所有代碼和它的工作原理,但增加了一些副作用。最初的「Line3」正在拉起一個應用程序列表來分享「某事」。第4行被添加到另一個特定的應用程序。現在發生的事情是,如果我點擊Line4,應用程序打開,一切都很好,直到我點擊後退按鈕返回到原始應用程序,我看到共享'Something'的應用程序列表也打開了,這意味着我有單擊後退按鈕兩次。

這是我的代碼(相當於上面的第二個例子)

/**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

你忘了R.id.TheProblemStuff:外殼內的return true;線,所以它繼續在R.id.TheOtherProblemStuff案件,只有然後停止。