2015-07-19 67 views
1

後無法返回主菜單我試圖將後退按鈕添加到操作欄以允許用戶在單擊菜單項後返回主活動。轉到菜單項

我將getActionBar().setDisplayHomeAsUpEnabled(true);添加到兩個菜單項活動中。

在我MainActivity.java我每個菜單項創建新意向:

@Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // action bar actions click 
     switch (item.getItemId()) { 
     case R.id.action_settings: 
      // Selected settings menu item 
      // launch Settings activity 
      Intent intent = new Intent(MainActivity.this, 
        SettingsActivity.class); 
      startActivity(intent); 
      return true; 

     case R.id.action_about: 
      // Selected about item 
      // launch about activity 
      Intent i = new Intent(MainActivity.this, 
        AboutActivity.class); 
      startActivity(i); 
      return true; 
     default: 
      return super.onOptionsItemSelected(item); 
     } 
    } 

logcat,當我點擊操作欄後面的圖標我得到這個錯誤:

W/AudioTrack﹕ AUDIO_OUTPUT_FLAG_FAST denied by client 

在尋找/尋找答案後,it was suggested the following change

... 
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
startActivity(intent); 

但它在我的情況下不起作用。我也嘗試使用finish()startActivity,但它沒有工作,也打破了設備後退按鈕

我是新來的android和欣賞,如果你解釋爲什麼我不能去主要活動一旦我在這種情況下的新活動。

更新#1:新增MainActivity.java:(我下面this tutorial

public class MainActivity extends Activity { 
    private static final String TAG = MainActivity.class.getSimpleName(); 
    private DrawerLayout mDrawerLayout; 
    private ListView mDrawerList; 
    private ActionBarDrawerToggle mDrawerToggle; 

    // Navigation drawer title 
    private CharSequence mDrawerTitle; 
    private CharSequence mTitle; 
    private List<Category> albumsList; 
    private ArrayList<NavDrawerItem> navDrawerItems; 
    private NavDrawerListAdapter adapter; 

    @SuppressLint("NewApi") 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     mTitle = mDrawerTitle = getTitle(); 

     mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout); 
     mDrawerList = (ListView) findViewById(R.id.list_slidermenu); 

     navDrawerItems = new ArrayList<NavDrawerItem>(); 

     // Getting the albums from shared preferences 
     albumsList = AppController.getInstance().getPrefManger().getCategories(); 

     // Insert "Recently Added" in navigation drawer first position 
     Category recentAlbum = new Category(null, 
       getString(R.string.nav_drawer_recently_added)); 

     albumsList.add(0, recentAlbum); 

     // Loop through albums in add them to navigation drawer adapter 
     for (Category a : albumsList) { 
      navDrawerItems.add(new NavDrawerItem(a.getId(), a.getTitle())); 
     } 

     mDrawerList.setOnItemClickListener(new SlideMenuClickListener()); 

     // Setting the nav drawer list adapter 
     adapter = new NavDrawerListAdapter(getApplicationContext(), 
       navDrawerItems); 
     mDrawerList.setAdapter(adapter); 

     // Enabling action bar app icon and behaving it as toggle button 
     getActionBar().setDisplayHomeAsUpEnabled(true); 
     getActionBar().setHomeButtonEnabled(true); 
     getActionBar().setIcon(
       new ColorDrawable(getResources().getColor(
         android.R.color.transparent))); 

     mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, 
       R.drawable.ic_drawer, R.string.app_name, R.string.app_name) { 
      public void onDrawerClosed(View view) { 
       getActionBar().setTitle(mTitle); 
       // calling onPrepareOptionsMenu() to show action bar icons 
       invalidateOptionsMenu(); 
      } 

      public void onDrawerOpened(View drawerView) { 
       getActionBar().setTitle(mDrawerTitle); 
       // calling onPrepareOptionsMenu() to hide action bar icons 
       invalidateOptionsMenu(); 
      } 
     }; 
     mDrawerLayout.setDrawerListener(mDrawerToggle); 

     if (savedInstanceState == null) { 
      // on first time display view for first nav item 
      displayView(0); 
     } 
    } 
+0

它看起來還有其他事情發生,在MainActivity中發佈onCreate()方法的內容。 –

+0

它看起來實際上只是一個警告:http://stackoverflow.com/questions/27820211/audio-output-flag-fast-denied-by-client你可以發佈完整的日誌,當你點擊後退按鈕時會發生什麼? –

+0

@DanielNugent catlog只是說明當我點擊後退按鈕時,沒有其他信息 –

回答

0

添加另一種情況:

case android.R.id.home: 
    onBackPressed(); 
    return true; 

這應該工作。

+0

不幸的是,它並沒有解決問題 –

+0

也,設備後退按鈕在所有情況下工作,但操作欄中的後退圖標不起作用 –

+0

試試這個: - case android.R.id.home: Intent homeIntent = new意圖(this,HomeActivity.class); homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); startActivity(homeIntent); –