Android - Back button in the title bar

前端 未结 26 2064
南方客
南方客 2020-12-04 07:02

In many apps (Calendar, Drive, Play Store) when you tap a button and enter a new activity, the icon in the title bar turns into a back button, but for the app I am making, i

相关标签:
26条回答
  • 2020-12-04 07:27

    The other answers don't mention that you can also set this in the XML of your Toolbar widget:

    app:navigationIcon="?attr/homeAsUpIndicator"

    For example:

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:navigationIcon="?attr/homeAsUpIndicator"
        app:popupTheme="@style/AppTheme.PopupOverlay"
        app:title="@string/title_activity_acoustic_progress" />
    
    0 讨论(0)
  • 2020-12-04 07:28
    Toolbar toolbar=findViewById(R.id.toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    
    if (getSupportActionBar()==null){
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowHomeEnabled(true);
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
    if(item.getItemId()==android.R.id.home)
        finish();
    return super.onOptionsItemSelected(item);
    }
    
    0 讨论(0)
  • 2020-12-04 07:28

    This is working for me.. Suppose there are two activity (Activityone,Activitytwo)

    Inside Activitytwo use this code

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    

    On Activityone

    //when you need to go second activity
    startActivity(new Intent(Activityone.this, Activitytwo.class));
    

    This should be included in second activity inside manifest file

    <activity android:name=".Activitytwo"
            android:parentActivityName=".Activityone"></activity>
    

    And The result would be like this

    0 讨论(0)
  • 2020-12-04 07:29

    The simplest way and best practice as google explains in here :

    1.Add a parent for your childActivity in the AndroidManifest.xml :

    <activity 
            android:name=".ChildActivity"
            android:parentActivityName=".ParentActivity" >
    </activity>
    

    2.Activate the back button in your childActivity :

    myActionOrActionSupportBar.setDisplayHomeAsUpEnabled(true);
    

    Worked for me, I hope it works for you too.

    0 讨论(0)
  • 2020-12-04 07:29

    Just sharing something that helped me and may be useful for others. Although most of the answers here are correct, by using the getActionBar().setDisplayHomeAsUpEnabled(true);, this wasn't working for me. The issue I had was that I was trying to create a second Activity manually, but there are more details involved.
    What really solved my problem was following Android Developers tutorial (https://developer.android.com/training/basics/firstapp/starting-activity) for creating a second Activity using Android Studio own tools:

    Create the second activity
    1. In the Project window, right-click the app folder and select New > Activity > Empty Activity.
    2. In the Configure Activity window, enter "DisplayMessageActivity" for Activity Name and click Finish (leave all other properties set to the defaults).
    Android Studio automatically does three things:
    - Creates the DisplayMessageActivity file.
    - Creates the corresponding activity_display_message.xml layout file.
    - Adds the required <activity> element in AndroidManifest.xml.
    
    0 讨论(0)
  • 2020-12-04 07:29

    All you need to do in 2020:
    (considering you want to return to the MainActivity)

    protected void onCreate(Bundle savedInstanceState){
        ...
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    
    public boolean onOptionsItemSelected(MenuItem item) {
        Intent myIntent = new Intent(getApplicationContext(), MainActivity.class);
        startActivityForResult(myIntent, 0);
        return true;
    }
    
    0 讨论(0)
提交回复
热议问题