Finish Parent and Current Activity in Android

Finish parent and current activity in Android

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.

How to finish parent activity from child activity

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();

}
}
}

Android finish current activity and start parent activity

I simply did something like this :

 Intent intent = new Intent();
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();

This did the trick.

Android - Starting a finished parent activity from child activity onResume()

Add launchMode="singleTask" for the MainActivity in AndroidManifest file..

 <activity
android:name=".MainActivity"
android:launchMode="singleTask"/>

Finish all activities at a time

Whenever you wish to exit all open activities, you should press a button which loads the first Activity that runs when your application starts then clear all the other activities, then have the last remaining activity finish.
to do so apply the following code in ur project

Intent intent = new Intent(getApplicationContext(), FirstActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);

The above code finishes all the activities except for FirstActivity.
Then we need to finish the FirstActivity's
Enter the below code in Firstactivity's oncreate

if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}

and you are done....

How can I return to a parent activity correctly?

You declared activity A with the standard launchMode in the Android manifest. According to the documentation, that means the following:

The system always creates a new instance of the activity in the target
task and routes the intent to it.

Therefore, the system is forced to recreate activity A (i.e. calling onCreate) even if the task stack is handled correctly.

To fix this problem you need to change the manifest, adding the following attribute to the A activity declaration:

android:launchMode="singleTop"

Note: calling finish() (as suggested as solution before) works only when you are completely sure that the activity B instance you are terminating lives on top of an instance of activity A. In more complex workflows (for instance, launching activity B from a notification) this might not be the case and you have to correctly launch activity A from B.

Finish Current Activity and start a new one

try this code for start browser and clear all the stacks of your application

Intent intent  = new Intent(); // need to set your Intent View here
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
getActivity().startActivity(intent);
getActivity().finish();

Updated

Try this attribute in your activity in AndroidManifiest.xml file

<activity android:name=".MainActivity"
android:excludeFromRecents="true" ...


Related Topics



Leave a reply



Submit