Proper way to handle action bar up button?

后端 未结 6 1937
没有蜡笔的小新
没有蜡笔的小新 2020-12-02 20:50

I use ActionBarSherlock (although I don\'t think it matters).

I have a Main activity and an About activity. I want the About activity to show the back-arrow by its l

6条回答
  •  孤街浪徒
    2020-12-02 21:27

    Have you also tried this (taken from Android website here) :

    in the manifest, for each activity X that needs to go to the main activity, add this to the code:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        // Respond to the action bar's Up/Home button
        case android.R.id.home:
            NavUtils.navigateUpFromSameTask(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
    

    and this to its manifest xml tag:

    
    

    if you need to still have the same state on the main activity, use this code instead :

    Intent intent = NavUtils.getParentActivityIntent(this); 
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP); 
    NavUtils.navigateUpTo(this, intent);
    

    if the API is 16 or above, you can add an attribute of parentActivityName with the path to the main activity instead of the metadata .

提交回复
热议问题