How do I restart the app when it’s brought from the background?

Asked

Viewed 72 times

1

I need my app to reboot whenever it’s brought from the OS background, so the user is forced to log in again.

I tried to use the finishAffinity() in my onPause() of Activity, the problem is that this method is also called when there is a transition between activities, causing when the OnBackPressed if the application were to restart as well.

1 answer

2


If there is no other solution, use a flag to indicate when to do Finish.

As I understand it just should not happen when it returns from an Activity launched by her.

Declare the flag as an attribute

Boolean canFinish = true;

When to launch Activity

canFinish = false;
startActivity(....);

In the method onPause()

if(canFinish){
    finishAffinity()
}else{
    canFinish = true;
}

I assume that finishAffinity() does what you intend and should be done in the onPause().

Browser other questions tagged

You are not signed in. Login or sign up in order to post.