How to make the button back in android studio?

0

Hello friends, I would like to know how I can create the back button in the ActionBar in android studio, as well as the image, which when pressed will bring me back to the previous class. I hope you can help me and thank you very much.

    
asked by Spartan456 03.06.2017 в 22:08
source

1 answer

2

In the manifest for each activity you declare which is father.

<application ... >
    ...
    <!-- The main/home activity (it has no parent activity) -->
    <activity
        android:name="com.example.myfirstapp.MainActivity" ...>
        ...
    </activity>
    <!-- A child of the main activity -->
    <activity
        android:name="com.example.myfirstapp.DisplayMessageActivity"
        android:label="@string/title_activity_display_message"

android: parentActivityName="com.example.myfirstapp.MainActivity" >                       

And for the button to appear, in the onCreate () of the activities you have to add this line:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

or

getActionBar().setDisplayHomeAsUpEnabled(true);

if you do not use the compatibility version.

And in the children's activities to capture the event and return to the father:

@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);
}

Ref: Providing Up Navigation from Android Developers

    
answered by 04.06.2017 / 02:03
source