Search code examples
androidrestart

Restart app after 5s when open app from recent application


I click button home or recent application to close app and then I open app from recent application. Now I want if time I close app larger 5s my app will restart. How to do it? thank everyone


Solution

  • You can achieve this by SharedPreferences and implementing own logic, like when you will press home button store the current time in onPause() method and when the user opens the app again then check with the condition with the current time in onResume() and the stored value, if its more than 5s then restart it.

    One thing remember, when restarting the activity store some negative number in SharedPreference so that, it won't restart again. And to restart the activity write below logic.

    Intent intent = getIntent();
    finish();
    startActivity(intent);