2015-10-06 78 views
2

我只想在我的主要活動中顯示我的菜單,然後從我的其他活動而不是菜單中獲得後退按鈕。現在我只是想知道如何從操作欄中刪除我不想要的活動的菜單。如何僅在主要活動中顯示菜單?

我的清單:我的主要活動中

<?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> 

我的菜單揭幕戰:

@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); 
} 
+0

簡單的'不要附加菜單'的其他活動..! – AndiGeeky

+0

@AndiGeeky我很抱歉,我會把這個放在哪裏?在清單中? – James

+1

只需從您的活動'onCreateOptionsMenu'中移除此方法.. !! – AndiGeeky

回答

1

如果你想在你的任何活動的菜單,然後你需要重寫onCreateOptionsMenuonOptionsItemSelected ... 如果您不要菜單,只是不要在你的活動中覆蓋這些方法...

+0

乾杯,我覺得這是最簡單的解決方案,雖然在我的其他活動中,我只是忽略了CreateOptionsMenu。如果沒有菜單點擊,是否有任何理由重寫onOptionsItemSelected? – James

+0

onOptionsItemSelected()用於處理菜單項單擊。 –

2

Activity.onCreateOptionsMenu是它被創建的地方。只是不要重寫這個方法,或者讓它返回false來不顯示菜單。

您必須返回true才能顯示菜單;如果您返回false,則不會顯示。

如果您使用的是所有的活動相同的實現,定義一個字段boolean isMain和onCreateOptionsMenu

public boolean onCreateOptionsMenu (Menu menu) { 
    if (!isMain) 
     return false; 

    [creating the menu here like before] 
} 
+0

非常感謝!!isMain部分應該是這樣!是(我的活動)還是更類似於(class == mainactivity)? – James

+0

@James我不知道你的實現,所以我不能真正建議任何事情。但任何有效的東西都會爲你做。只需使用您已有的信息來區分不同的活動。 – tynn

0

返回它對於您創建的每一項活動,都必須有相關的菜單文件它在res/menu文件夾中。只需刪除或註釋相應的<item>標籤。您可以通過查看預覽屏幕中的輸出顯示來試驗它們。

注意:我認爲這隻適用於使用UI自動創建活動(即不是手動),因爲相應的菜單文件是自動生成的。

0

您需要使用以下步驟來刪除選項菜單在非主要活動添加後退按鈕 -

  1. 不要重寫onCreateOptionsMenu方法。
  2. 致電getActionBar()。setDisplayHomeAsUpEnabled(true)在您的activity的onCreate方法中。在以下
  3. 覆蓋onOptionsItemSelected方法方式 -

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
    switch(item.getItemId()) { 
        case android.R.id.home: 
         finish(); 
         return super.onOptionsItemSelected(item); 
        } 
    } 
    
相關問題