简体   繁体   中英

How to call onDestroy() from onStop() after a delay

If the user clicks on the Home button for example, the methods onPause() and onStop() are called. I want to call onDestroy() from the onStop() method after 1mn, unless the user goes back on the app (which calls onResume() and onStart() methods).

I tried to implement Timer: It fails, saying it cannot call the onDestroy if Looper not implemented. When I implement the Looper, the onDestroy() method is never called.

Maybe calling onDestroy() from onStop() is not the good thing to do, and another "clean" solution exists to get the behavior I want. I just want to kill the app after 1mn no use. In this case, please propose.

If my wish is the good way to proceed, could you share how to implement it ?

dont call onDestroy() directly , instead call finish() after the period you want and to support the scenario you mentioned make sure not to kill the activity if the user resumed the activity here's a piece of code i wrote for you . the activity will kill its self if not resumed in 1 second ;

boolean notResumed;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startActivity(new Intent(this,Main2Activity.class));
}

@Override
protected void onResume() {
    super.onResume();
    notResumed=false;
}

@Override
protected void onStop() {
    super.onStop();
    notResumed=true;
    Handler handler=new Handler();
    handler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if(notResumed)
            finish();
        }
    },1000);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.d("debug","onDestroyCalled");
}

This answer is largely inspired from Abdelrahman's post above. I just adapted few things to reinitialize the delay counter each time I go out of my app.

boolean notResumed;
//Declare my Handler in global to be used also in onResume() method
Handler myHandler;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    startActivity(new Intent(this,Main2Activity.class));
}

@Override
protected void onResume() {
    super.onResume();
    notResumed=false;
    //Remove callbacks on the handler if it already exists
    if (myHandler != null) {
        //I send null here to remove all callbacks, all messages,
        //and remove also the reference of the runnable
        myHandler.removeCallbacks(null);
    }
}

@Override
protected void onStop() {
    super.onStop();
    notResumed=true;
    myHandler=new Handler();
    myHandler.postDelayed(new Runnable() {
        @Override
        public void run() {
            if(notResumed)
                finish();
        }
    },10000);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.d("debug","onDestroyCalled");
}

Thanks a lot again to Abdelrahman Nazeer for its fast and accurate answer. Please comment if something is not correctly done here. At least it works as expected...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM