Search code examples
androidactivity-lifecycle

Run oncreate every time android app is opened


I have made an android app, when I close the app (using the home button) and reopen the app, it does not call onCreate() again. Instead it just loads from memory.

How can I make sure every time the app is opened onCreate() is executed?


EDIT: The app has to do the same thing in onCreate() as in onResume().

When I copy the exact code to the function onResume() it does not work the same.

Therefore I want to close the app (and the process) completely (or do something similar) so onCreate() always gets called after reopening the app.


Solution

  • I understand that you want onCreate() to be called everytime app opens up butit does not. This is because your app will still be in recents when you are opening it again.

    1. When you open app when it already exists in recents, your onStart() gets called. Try adding your logic in onStart().
    2. If you want only onCreate() to be called everytime, then you must remove from recents when home is pressed. For that you need to make changes in Manifest fie for the activity.Try using the below for the activity inManifets :

      android:clearTaskOnLaunch="true"

      android:finishOnTaskLaunch="true"