Android: Go back to previous activity

前端 未结 23 1989
抹茶落季
抹茶落季 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:37

    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.
    

提交回复
热议问题