android toolbar: how to go back to previous activi

2020-07-13 10:59发布

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.

5条回答
倾城 Initia
2楼-- · 2020-07-13 11:24

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);
}
查看更多
你好瞎i
3楼-- · 2020-07-13 11:26

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>
查看更多
【Aperson】
4楼-- · 2020-07-13 11:28
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
查看更多
甜甜的少女心
5楼-- · 2020-07-13 11:33

You can simply use:

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

to set up your backbutton on toolbar.

In Manifest:

<activity
    android:name="Your Second Activity"
    android:parentActivityName="Your First Activity" >
    <!-- Parent activity meta-data to support 4.0 and lower -->
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="Your First Activity" />
</activity>

And finally use:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) 
{
   NavUtils.navigateUpFromSameTask(this);
}

return super.onOptionsItemSelected(item);
}
查看更多
叼着烟拽天下
6楼-- · 2020-07-13 11:36

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

查看更多
登录 后发表回答