Add back button to action bar

后端 未结 11 1689
野趣味
野趣味 2020-12-07 16:23

I have been trying to add a back button to the action bar.

I want my view to look like this: \"enter

相关标签:
11条回答
  • 2020-12-07 16:49

    There are two ways to approach this.

    Option 1: Update the Android Manifest If the settings Activity is always called from the same activity, you can make the relationship in the Android Manifest. Android will automagically show the 'back' button in the ActionBar

    <activity
        android:name=".SettingsActivity"
        android:label="Setting Activity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.example.MainActivity" />
    </activity>
    

    Option 2: Change a setting for the ActionBar If you don't know which Activity will call the Settings Activity, you can create it like this. First in your activity that extends ActionBarActivity (Make sure your @imports match the level of compatibility you are looking for).

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_settings_test);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setHomeButtonEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
    }
    

    Then, detect the 'back button' press and tell Android to close the currently open Activity.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // app icon in action bar clicked; goto parent activity.
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    

    That should do it!

    0 讨论(0)
  • 2020-12-07 16:51

    After setting

     actionBar.setHomeButtonEnabled(true);
    

    You have to configure the parent activity in your AndroidManifest.xml

    <activity
        android:name="com.example.MainActivity"
        android:label="@string/app_name"
        android:theme="@style/Theme.AppCompat" />
    <activity
        android:name="com.example.SecondActivity"
        android:theme="@style/Theme.AppCompat" >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.MainActivity" />
    </activity>
    

    Look here for more information http://developer.android.com/training/implementing-navigation/ancestral.html

    0 讨论(0)
  • 2020-12-07 16:53

    this one worked for me:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_your_activity);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        // ... other stuff
    }
    
    @Override
    public boolean onSupportNavigateUp(){
        finish();
        return true;
    }
    

    The method onSupportNavigateUp() is called when you use the back button in the SupportActionBar.

    0 讨论(0)
  • 2020-12-07 16:57

    if anyone else need the solution

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
    
        if (id == android.R.id.home) {
            onBackPressed();
        }
    
        return super.onOptionsItemSelected(item);
    }
    
    0 讨论(0)
  • 2020-12-07 16:57

    Add this line in onCreate() method

    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    

    then Override this method

     @Override
        public boolean onSupportNavigateUp(){
            finish();
            return true;
        }
    
    0 讨论(0)
  • 2020-12-07 17:07

    After setting actionBar.setHomeButtonEnabled(true);

    Add the following code:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // app icon in action bar clicked; goto parent activity.
                this.finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    
    0 讨论(0)
提交回复
热议问题