Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android toolbar: how to go back to previous activity if back arrow is pressed

I have an activity called Place

I come to Place activity from its previous activity called City.

I have added back button to the toolbar in Place activity using the following code:

    Toolbar mToolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true)

I want to set the back button to go back to City activity

But City activity needs some parameters to be passed to it.

So how to tell the back button to go to City activity, without need to pass the parameters to it.

like image 348
Santhosh Avatar asked Mar 17 '16 12:03

Santhosh


People also ask

How do I go back to previous activity on android?

You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.

What is onSupportNavigateUp?

onSupportNavigateUp() This method is called whenever the user chooses to navigate Up within your application's activity hierarchy from the action bar. ActionMode. onWindowStartingSupportActionMode(ActionMode.Callback callback) Called when a support action mode is being started for this window.

What is action bar android?

Android ActionBar is a menu bar that runs across the top of the activity screen in android. Android ActionBar can contain menu items which become visible when the user clicks the “menu” button.


3 Answers

According to your Question.You should use finish ()

Call this when your activity is done and should be closed.To finish an activity finish() method should be called. This is applicable for the currently active activity.

Example

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) // Press Back Icon
    {
        finish();
    }

    return super.onOptionsItemSelected(item);
}
like image 104
IntelliJ Amiya Avatar answered Oct 16 '22 10:10

IntelliJ Amiya


In your AndroidManifest.xml file, add tag android:parentActivityName and point it to where you want to go on back button. In java you need to call

 getActionBar().setDisplayHomeAsUpEnabled(true);

method, which you have already done. This will help.

<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" >
    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
like image 7
Alok Gupta Avatar answered Oct 16 '22 09:10

Alok Gupta


In your backButton you just call finish(); and you close Activity without need to pass parameter to other Activity.

like image 5
Kristiyan Varbanov Avatar answered Oct 16 '22 09:10

Kristiyan Varbanov