How to handle “Up” button?

前端 未结 2 1719
刺人心
刺人心 2020-12-08 20:10

How to handle \"Up\" button (SDK version 11+)? I am referring to the one at the top of screen, that holds the application icon.

In Android Design articles it was nam

相关标签:
2条回答
  • 2020-12-08 20:53

    Implement onOptionsItemSelected() and watch for android.R.id.home "menu" events, as is described in the documentation.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // do something useful
                return(true);
        }
    
        return(super.onOptionsItemSelected(item));
    }
    
    0 讨论(0)
  • 2020-12-08 21:03

    First change your AndroidManifest.xml file to have a parent activity declared. Eg

        <activity android:name=".theory"
                  android:parentActivityName=".MainActivity"
            android:label="@string/theory"
            />
        <activity android:name=".experimental"
                  android:parentActivityName=".MainActivity"
            android:label="@string/exp"
            />
    

    Do this for all the activities other than the MainActivity. Note the parentActivityName xml code

    Then go to the respective java files and add the following code

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

    You have you up button enabled now.

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