Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to finish parent activity from child activity

Tags:

I am new to Android development.

I have created a main Activity (->A), which has 4 buttons. One of the 4 buttons is the EXIT-button.

I start another activity (->B), on click of the EXIT-button. This opens 'B'Activity via an intent from 'A'Activity.

Activity 'B' contains - Do you want to exit? Yes-Button & No-Button.

If I give finish(), onclick of the button - it exits the 'B'Activity. I want to finish 'B' & 'A'.

I have even tried A.finish() -> this doesn't get recognized and results in syntax/semantic error.

I would appreciate help here.

P.S : I am using Android-2.2 version phone, and I do not like to use ActivityManager to resolve this.

like image 861
sudhishkr Avatar asked Mar 12 '12 08:03

sudhishkr


People also ask

How to close parent Activity in Android?

use startActivityForResult() in Activity A for start activity B and onActivityResult() in A just finish() Activity A. In Activity B on Button pressed just finish() Activity B.

How do you finish an activity?

On Clicking the back button from the New Activity, the finish() method is called and the activity destroys and returns to the home screen.

How to finish Current and previous Activity in Android?

You can use the Intent flag FLAG_ACTIVITY_CLEAR_TOP to restart an activity from the stack and clear everything that was above it.


1 Answers

Try to launch child activity with

 startActivityForResult(intent, REQUEST_EXIT); 

In child activity

case R.id.quit:      setResult(RESULT_OK, null);      finish(); 

In parent activity

 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {      if (requestCode == REQUEST_EXIT) {          if (resultCode == RESULT_OK) {             this.finish();           }      } } 
like image 62
Georgy Gobozov Avatar answered Oct 24 '22 07:10

Georgy Gobozov