Android: Go back to previous activity

前端 未结 23 1574
抹茶落季
抹茶落季 2020-11-22 06:52

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

相关标签:
23条回答
  • 2020-11-22 07:19

    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.

    0 讨论(0)
  • 2020-11-22 07:20

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

    0 讨论(0)
  • 2020-11-22 07:21

    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);
    
    0 讨论(0)
  • 2020-11-22 07:22

    Besides all the mentioned answers, their is still an alternative way of doing this, lets say you have two classes , class A and class B.

    Class A you have made some activities like checkbox select, printed out some data and intent to class B. Class B, you would like to pass multiple values to class A and maintain the previous state of class A, you can use, try this alternative method or download source code to demonstrate this

    http://whats-online.info/science-and-tutorials/125/Android-maintain-the-previous-state-of-activity-on-intent/

    or

    http://developer.android.com/reference/android/content/Intent.html

    0 讨论(0)
  • 2020-11-22 07:23

    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.

    0 讨论(0)
  • 2020-11-22 07:25
    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

    0 讨论(0)
提交回复
热议问题