Troj
Troj

Reputation: 11911

Android: Go back to previous activity

I want to do something simple on android app. How is it possible to go back to a previous activity.

What code do I need to go back to previous activity

Upvotes: 611

Views: 787322

Answers (23)

Akash Jaiswal
Akash Jaiswal

Reputation: 31

To go back from one activity to another by clicking back button use the code given below use current activity name and then the target activity.

@Override
public void onBackPressed() {
    // do something on back.
    startActivity(new Intent(secondActivity.this, MainActivity.class));
    return;
}

Upvotes: 0

Monir Zzaman
Monir Zzaman

Reputation: 489

First, thing you need to keep in mind that, if you want to go back to a previous activity. Then don't call finish() method when goes to another activity using Intent.

After that you have two way to back from current activity to previous activity:

Simply call:

finish()

OR

super.onBackPressed();

Upvotes: 1

Muhammed Fasil
Muhammed Fasil

Reputation: 8556

Try this is act as you have to press the back button

finish();
super.onBackPressed();

Upvotes: 16

Gyan Swaroop Awasthi
Gyan Swaroop Awasthi

Reputation: 699

There are few cases to go back to your previous activity:

Case 1: if you want take result back to your previous activity then ActivityA.java

 Intent intent = new Intent(ActivityA.this, FBHelperActivity.class);
               startActivityForResult(intent,2);

FBHelperActivity.java

 Intent returnIntent = new Intent();
 setResult(RESULT_OK, returnIntent);
 finish();

Case 2: ActivityA --> FBHelperActivity---->ActivityA

ActivityA.java

 Intent intent = new Intent(ActivityA.this, FBHelperActivity.class);
               startActivity(intent);

FBHelperActivity.java

after getting of result call finish();
 By this way your second activity will finish and because 
 you did not call finish() in your first activity then
 automatic first activity is in back ground, will visible.

Upvotes: 3

NoNaMe
NoNaMe

Reputation: 6222

Got the same problem and

finish();  OR super.onBackPressed();

worked fine for me, both worked same, but no luck with return

Upvotes: 10

Darshuu
Darshuu

Reputation: 511

Add this in your onCLick() method, it will go back to your previous activity

finish();

or You can use this. It worked perfectly for me

 @Override
  public boolean onOptionsItemSelected(MenuItem item) {
  int id = item.getItemId();

      if ( id == android.R.id.home ) {
         finish();
         return true;
       }

  return super.onOptionsItemSelected(item);
  }

Upvotes: 15

byteC0de
byteC0de

Reputation: 5273

Just try this in, first activity

Intent mainIntent = new Intent(Activity1.this, Activity2.class);
this.startActivity(mainIntent);

In your second activity

@Override
public void onBackPressed() {
    this.finish();
}

Upvotes: 0

Abhinav
Abhinav

Reputation: 39884

Android activities are stored in the activity stack. Going back to a previous activity could mean two things.

  1. 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.

  2. Keep track of the activity stack. Whenever you start a new activity with an intent you can specify an intent flag like FLAG_ACTIVITY_REORDER_TO_FRONT or FLAG_ACTIVITY_PREVIOUS_IS_TOP. You can use this to shuffle between the activities in your application. Haven't used them much though. Have a look at the flags here: http://developer.android.com/reference/android/content/Intent.html

As mentioned in the comments, if the activity is opened with startActivity() then one can close it with finish(). If you wish to use the Up button you can catch that in onOptionsSelected(MenuItem item) method with checking the item ID against android.R.id.home unlike R.id.home as mentioned in the comments.

Upvotes: 553

Puni
Puni

Reputation: 1294

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
      int id = item.getItemId();

      if ( id == android.R.id.home ) {
          finish();
          return true;
      }

      return super.onOptionsItemSelected(item);
 }

Try this it works both on toolbar back button as hardware back button.

Upvotes: 3

wuxue
wuxue

Reputation: 113

I suggest the NavUtils.navigateUpFromSameTask(), it's easy and very simple, you can learn it from the google developer.Wish I could help you!

Upvotes: 3

Altaf Hussain
Altaf Hussain

Reputation: 5202

Start the second activity using intent (either use startActivity or startActivityForResult according to your requirements). Now when user press back button, the current activity on top will be closed and the previous will be shown.

Now Lets say you have two activities, one for selecting some settings for the user, like language, country etc, and after selecting it, the user clicks on Next button to go to the login form (for example) . Now if the login is unsuccessful, then the user will be on the login activity, what if login is successful ?

If login is successful, then you have to start another activity. It means a third activity will be started, and still there are two activities running. In this case, it will be good to use startActivityForResult. When login is successful, send OK data back to first activity and close login activity. Now when the data is received, then start the third activity and close the first activity by using finish.

Upvotes: 6

Ram G.
Ram G.

Reputation: 3125

if you want to go to just want to go to previous activity use

finish();

OR

onBackPressed();

if you want to go to second activity or below that use following:

intent = new Intent(MyFourthActivity.this , MySecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//Bundle is optional
Bundle bundle = new Bundle();
bundle.putString("MyValue1", val1);
intent.putExtras(bundle);
//end Bundle
startActivity(intent);

Upvotes: 14

Md Hussain
Md Hussain

Reputation: 409

@Override
public void onBackPressed() {
    super.onBackPressed();
}

and if you want on button click go back then simply put

bbsubmit.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        onBackPressed();
    }
});

Upvotes: 5

Akshay Paliwal
Akshay Paliwal

Reputation: 3916

Just call these method to finish current activity or to go back by onBackPressed

finish();

OR

onBackPressed();

Upvotes: 24

Dmitry Ryadnenko
Dmitry Ryadnenko

Reputation: 22512

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

This will get you to a previous activity keeping its stack and clearing all activities after it from the stack.

For example, if stack was A->B->C->D and you start B with this flag, stack will be A->B

Upvotes: 43

Umer Rana
Umer Rana

Reputation: 1047

Just write on click finish(). It will take you to the previous Activity.

Upvotes: 103

Thomas Decaux
Thomas Decaux

Reputation: 22651

If you have setup correctly the AndroidManifest.xml file with activity parent, you can use :

NavUtils.navigateUpFromSameTask(this);

Where this is your child activity.

Upvotes: 12

ameyx
ameyx

Reputation: 403

All new activities/intents by default have back/previous behavior, unless you have coded a finish() on the calling activity.

Upvotes: 5

nikki
nikki

Reputation: 3228

You can try this:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {

        finish();
        return true;
    }
    return super.onKeyDown(keyCode, event);
}

Upvotes: 5

AtanuCSE
AtanuCSE

Reputation: 8940

Just this

super.onBackPressed();

Upvotes: 70

Labeeb Panampullan
Labeeb Panampullan

Reputation: 34823

You can explicitly call onBackPressed is the easiest way
Refer Go back to previous activity for details

Upvotes: 8

adamp
adamp

Reputation: 28932

Try Activity#finish(). This is more or less what the back button does by default.

Upvotes: 253

Bryan Denny
Bryan Denny

Reputation: 27596

Are you wanting to take control of the back button behavior? You can override the back button (to go to a specific activity) via one of two methods.

For Android 1.6 and below:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
        // do something on back.
        return true;
    }

    return super.onKeyDown(keyCode, event);
}

Or if you are only supporting Android 2.0 or greater:

@Override
public void onBackPressed() {
    // do something on back.
    return;
}

For more details: http://android-developers.blogspot.com/2009/12/back-and-other-hard-keys-three-stories.html

Upvotes: 22

Related Questions