How to Close Activity and Go Back to Previous Activity in Android

Android: Go back to previous activity

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.

How to go back to Previous Activity?

You can do it by simply overriding onBackPressed method in your activity.

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

Hope this help!

How to close activity and go back to previous activity in android

I think you are calling finish() method in MainActivity before starting SettingsActivity.

The scenario which you have described will occur in following two ways:

EITHER

You have set android:noHistory = "true" for MainActivity inside AndroidManifest.xml which causes MainActivity to finish automatically on pressing the back key.

OR

Before switching to your 'SettingsActivity', you have called finish() in your MainActivity, which kills it. When you press back button,since no other activity is preset in stack to pop, it goes back to main screen.

Disable ability go back to previous activity

This will clear back task of application and when you press back button application will close. You need to add flags in Intent or you can use finish().

val intent = Intent(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
finish()

How to close Android activity and return to previous/main activity?

I want to understand how to simply RETURN to the previous activity

Don't call finish() in your first Activity. Then when the user presses the "Back" button the second Activity will be closed and taken off the stack and the onResum() of the first Activity will be called.

 Intent in = new Intent(MainActivity.this, settingmenu.class);

startActivity(in);
setResult(Activity.RESULT_OK);
// finish(); don't call this and the Activity will remain on the stack

As far as the crash, you would need to provide the logcat and quite possibly more of your code

You will probably want to read about the Stack which will go a long way in understanding how Activities are are placed on and popped off. Also, there is a very good talk from I believe Romain Guy at one of the older Google I/O conferences about this which is very helpful. I will try to post but you can Google those terms and you will find it.

Activity Docs

Google I/O Navigation and stack Not who I thought it was but definitely worth watching.

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

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

Go back to 2nd Previous Activity

override onbackpressed method and just do like this:-

 @Override
public void onBackPressed() {
Intent intent = new Intent(Activity3.this,
Activity1.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);

}

Enjoy....!

How to go previous Activity by using back button

Just Remove finish() from Activity.

Because when you go to second activity and finish first activity, there is no any activity and stack.

So If you click back button from second activity, application will be finish if there is no Activity in stack.

You should use this Approach.

Ex.

In Activity.java

Intent first = new Intent(Activity.this,ActivityB.class);
startAcivity(first);
// Don't use finish() here.

In ActivityB.Java

Just click on built in back button.

or If you want to use your own back button.

Use finish(); in button click event.



Related Topics



Leave a reply



Submit