5

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.

2
  • 2
    call finish() on clicklistner of back button Refer here for beautiful tutorials coderzpassion.com Mar 17, 2016 at 12:09
  • 1
    it worked. Thank you.
    – Santhosh
    Mar 17, 2016 at 12:10

5 Answers 5

23

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

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>
1
  • as i have mentioned my City activity needs to be passed some parameters. If i directly link my activity will not show anything because of some parameters are missing.
    – Santhosh
    Mar 17, 2016 at 12:25
5

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

2
  • after going back by finish() to the City activity. how to unselect the menu item in my navigation drawer view in City activity.
    – Santhosh
    Mar 17, 2016 at 12:28
  • when you make intent from City activity to Place activity your City activity is going to onPause() state and Place is uppear on UI when you press back button you kill Place activity and you bring back to City and in your method onPause() just select or unselect what you want. Mar 17, 2016 at 12:33
3
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
1
  • While this chunk of code may solve OP’s problem, it’s unclear why. I’d recommend to add some context on why this is necessary to do it this way. Aug 3, 2016 at 11:07
2

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

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.