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

家住魔仙堡 提交于 2019-12-06 04:29:19

问题


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.


回答1:


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




回答2:


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



回答3:


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>



回答4:


toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });



回答5:


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


来源:https://stackoverflow.com/questions/36060114/android-toolbar-how-to-go-back-to-previous-activity-if-back-arrow-is-pressed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!