Finish parent and current activity in Android

前端 未结 16 3069
误落风尘
误落风尘 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: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 :)

提交回复
热议问题