Proper way to handle action bar up button?

后端 未结 6 1910
没有蜡笔的小新
没有蜡笔的小新 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:11

    In your onCreate(Bundle savedInstanceState), do

    ActionBar actionBar = getSupportActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    

    Then in your onOptionsItemSelected(MenuItem item), do

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // go to previous screen when app icon in action bar is clicked
                Intent intent = new Intent(this, PreviousActivity.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    0 讨论(0)
  • 2020-12-02 21:12

    make sure your android:minSdkVersion="11" which could be seen in the manifest file, Up icon has been included from APK 11. I have made a small sample plz try the below link which may be help you just import into your work space

    http://www.mediafire.com/?hktdvts7yyduwv1

    0 讨论(0)
  • 2020-12-02 21:17

    For who want to just go back in the previous activity, this is the simplest solution:

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish(); //this method close current activity and return to previous
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
    
    0 讨论(0)
  • 2020-12-02 21:24

    Found out the root of my problem was a change in the manifest I made a while ago: I added this line:

    android:launchMode="singleInstance"
    

    so my main activity wouldn't be relaunched. Changing it to:

    android:launchMode="singleTask"
    

    solved my problems, and I was able to remove all the onOptionsItemSelected stuff. I knew there was something wrong, that Android should have been able to handle this better, and I was right. Check the manifest :P

    0 讨论(0)
  • 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:

    <meta-data
    android:name="android.support.PARENT_ACTIVITY"
    android:value="com.activities.MainActivity" />
    

    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 .

    0 讨论(0)
  • 2020-12-02 21:29

    You can Handle Action Bar button by using Code or XML.

    Check this code

    if you want it programmatically Add this line in onCreate() method

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    

    And Override this method onSupportNavigateUp()

    @Override
    public boolean onSupportNavigateUp(){  
    finish();  
    return true;  
    }
    

    OR Non-programmatically you can add meta to manifest file as

    <activity android:name="Current Activity"
            android:parentActivityName="Activity you want to open">
        </activity>
    

    Note: Make sure Actionbar is not null.

    0 讨论(0)
提交回复
热议问题