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.
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.
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.
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.
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);
}
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>
In your backButton
you just call finish();
and you close Activity without need to pass parameter to other Activity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With