Finish parent and current activity in Android

前端 未结 16 2980
误落风尘
误落风尘 2020-11-27 03:40

I have 3 activities. Activity A which leads to activity B, which in turn can go back to activity A or start activity C. However, if I press back in activity C the app should

相关标签:
16条回答
  • 2020-11-27 03:56

    Have each activity listen for a result from the next activity, and finish itself if that occurs.

    You can use a special result code to indicate that you want the activity to finish.

    0 讨论(0)
  • 2020-11-27 03:57

    If you want to finish a parent activity from a child activity,

    In the parent activity, while triggering the child activity, use the following command:-

    startActivityForResult(intent_name,any_integer_variable);
    

    and override the onActivityResult Method in the following manner:-

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(resultCode==2){
            finish();
        }
    }
    

    Now, in the child activity, override onStop and onDestroy in the following manner:-

    protected void onStop() {
        setResult(2);
        super.onStop();
    }
    @Override
    protected void onDestroy() {
        setResult(2);
        super.onDestroy();
    }
    

    Notice that I've set the value to 2 in the child activity, and am checking for it in the parent activity. If the value is the same that I have set, then the parent activity will also finish. Then you can use recursive approaches for further activities.

    0 讨论(0)
  • 2020-11-27 03:58

    You shou use onActivityResult method in your parent Activity

    Suppose Activity A is parent of Activity B. If you want to click back button in Activity B to exit Application (also exit Activity A)

    In your Activity B, in onStop() or onDestroy()

    you call

    setResult(0); //any int number is fine
    

    this will pass a result code to its parent activity.

    Your parent Actvity A, listens for the result code you will need to use onActivityResult method inside the method you can call

    if(resultCode == 0) //matches the result code passed from B
    {
        ActivityA.this.finish()
    }
    

    It works for me :)

    0 讨论(0)
  • 2020-11-27 04:00

    I don't know if this will work, but you could try it:

    • From Activity A, start activity B for a result using startActivityForResult()

    • In Activity B, when the user triggers Activity C, start activity C.

    startActivity() returns immediately, so

    • set a result that will inform A to finish as well,

    • Call finish() in B.

    • When A receives that result from B, A calls finish() on itself as well.

    Failing that, you could make Activity C into its own app and then close the first app (with A & B) after it starts the second.

    P.S. Take Falmarri's comment into consideration as you move forward!
    Good luck.

    0 讨论(0)
  • 2020-11-27 04:05

    From Activity A, start activity B for a result using startActivityForResult(intent,19)

    In Activity B, when the user triggers Activity C, start activity C.

    startActivity() returns immediately, so

    set a result that will inform A to finish as well,

    Call finish() in B. and Call setResult() method before finish() method in your Child Activity In Parent Activity overide onActivtyResult(....) method. When A receives that result from B, A calls finish() on itself as well.

    enter code here
         @Override
    protected void onActivityResult(int arg0, int arg1, Intent arg2) {
        //Added by Ashish to close parent activity
          if(arg1==19){
          finish();
            }
        super.onActivityResult(arg0, arg1, arg2);
    }
    
    0 讨论(0)
  • 2020-11-27 04:06

    You can use finishAffinity() , it will close Current & all Parent Activities of the Current Activity.

    Note : But any results from Child Activity to Parent Activity will not be handled.

    Hope, it helps !!

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