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

1

I need my app to restart whenever it is brought in from the OS background, so the user is required to log in again.

I tried to use finishAffinity() in my onPause() of activity, the problem is that this method is also called when there is a transition between activities , causing that when OnBackPressed was called the application would also restart .

    
asked by anonymous 08.03.2018 / 18:46

1 answer

2

If there is no other solution, use flag to indicate when to finish.

As I understand it, this should not happen when you return from an Activity launched by it.

Declare flag as an attribute

Boolean canFinish = true;

When you launch Activity

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

In method onPause()

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

Starting from the beginning that finishAffinity() does what you intend to do in onPause() .

    
08.03.2018 / 19:10