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.
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.
When you open app when it already exists in recents, your onStart() gets called. Try adding your logic in onStart().
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"
when I close the app (using the home button)
That does not "close the app", for any typical definition of "close". The closest analogy, in a desktop OS, would be minimizing the app.
How can I make sure every time the app is opened onCreate() is executed?
You don't. Instead, you use other lifecycle methods that are more appropriate, such as onStart(), which will be called both after onCreate() (when the activity is first created) and when the activity is returning to the foreground from having been in the background.
I have put System.exit(1) in the onStop() function. After opening the app from recent apps, it runs onCreate().
So this worked for me.
Just do call onResume this will do the trick.
#Override
protected void onResume() {
super.onResume();
// TODO: do what ever you want
}
Have a look at this site: Activity-lifecycle concepts
Related
I am new to Android, but after studying the Activity Lifecycle, I understood that if I minimise the app, it should call onPause(), and while I reopen it should call onResume(). But, in my case, it calls onCreate() first and then onResume(). This is causing my widgets and other variables to enter wrong state.
My app only has an activity.
Why is the onCreate() method being invoked before onResume()?
It is possible that the app process is killed by the system either from onPause() or onStop() to create room in memory for other apps in the foreground, in which case when you reopen the activity, it will be created. In that case onCreate() -> onStart() -> onResume() is the expected sequence. When the activity is no longer visible, onStop() will be called, so when you navigate back to the activity onStart() -> onResume() is what takes place.
More on activity lifecycle here.
Additionally, since Android 10 there are some restriction of apps running from background. And "an app running a foreground service is considered to be running in the background" which is what you are describing. That may be why it is destroyed and app lifecycle starts from onCreate()
If I understand the issue correctly, it sounds like you're receiving a new instance of your Activity when you're looking to resume it instead. You can change how Activities are handled by setting their launch mode.
To resume an Activity if it already exists you could change its launchMode value in your Manifest to something like this:
<activity
android:name=".MySingleInstanceActivity"
android:launchMode="singleTask" />
There are several launch modes available and there may be one that's better suited for your project. You can read more about tasks and launch modes at https://developer.android.com/guide/components/activities/tasks-and-back-stack#TaskLaunchModes.
I want following behavior in my app:
1) Show notification on its launch: done
2) Do not show notification while switching through activities of the app
3) If another app is launched over this app, while coming back to the app again show notification.
I am planning to implement it via shared prefs(by storing a boolean value in it),
But cannot figure out the place to reset this value
i.e. how to know that app is exiting or switching to another app to reset this value(so it can show notification when app is launched again).
When an Activity goes to the background, onPause() is called, when it comes back to the foreground, onResume() is called. You can use that states to hide/show your notifications or even store your SharedPreferences.
Have a look at the Activity Lifecylce:
If you want to detect if all application's activities gone to background you need to extend Application class and register activity lifecycle listeners. Then just count calls to onPause() and onCreate() method.
http://developer.android.com/reference/android/app/Application.html#registerActivityLifecycleCallbacks(android.app.Application.ActivityLifecycleCallbacks)
Try to understand Android application life cycle.
Go through the following methods documentation.
onCreate() , onStart(), onPause() , onResume() , onDestroy() , onRestart()
Link
3) If another app is launched over this app, while coming back to the
app again show notification again.
To do this you need to overwrite back button and check activity stack. If it is 0 - show notification.
On start my app displays a splash screen and checks via network if the current user is still premium.
My problem: I started my app right before I went to bed and minimized it by pressing the home button. In the morning I launched the app again and it resumed the activity from the night. The app never really quit, my splash screen was not shown and and it couldn't check if the user is still premium.
So how can I achieve my app to be closed after a certain time (e.g. when the app is minimized)?
You should write the Premium user check logic in your onResume() method so that
if the activity is in pause or background state it will check the
logic every time it will be launched .
Don't try to finish app when it's minimized. Use Activity lifecycle callbacks.
#Override
protected void onResume (){
//check for changes here
}
If you want to end an activity, you call finish(). So you could record the time in onPause, then in onResume, check how long its been. If its been too long, call startActivity on your main activity, then call finish() to end the old one.
I think you should become more familiar with the Android Activity Lifecycle and think about which call back in your activity should you check if the user is premium
I have an app which the moment the user starts running the app it register a new entry in a database. The thing is that when the user quits I would like to delete this registry in the database. I know doing al these inserts and deletes with JSON, MySQL and PHP but I dont know how to apply these functions when the user quits the application in the normal way.
I would like to know if there is a function like public onBackPressed() where it does always the same thing but you can overwrite it.
Thanks
There is a method onDestroy() which is called when user is done with application and resources are freed, you can override it in your activity and do whatever you want in it, if you want a one step previous from onDestroy() it is onStop() which is called when activity goes background etc. You can override it as well.
use Android's onBackPressed() method and remove the super call and do all your operations and at the last stage, write finish(), this will close your application.
This will work only if your other activities are closed, else it will close the current activity and go to the last activity.
Here is the scenario:
I have two Activities. Lets name them Activity A and Activity B.
Say Activity A is open. Now, when I go and open Activity B, Activity A is closed because the onStop() method is called.
Now, when I flip back to Activity A, the onCreate() method is called, but I want the onRestart() method called instead. How do I do this?
You cannot influence the livecycle of your app like that. There should be no reason to rely on onRestart(). If you use onStart() it will always be called no matter if the Android OS killed the app process in the background.
Check out this doc for further information:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Damn beat me to it but here goes anyway
According to the Activity Lifecycle onCreate() is called again if the Activity was removed from memory because the OS deemed that another app needed the memory. In this case, you can't ensure that onRestart() will always be called for your Activity.
Like already stated you must find a different way of achieving your goal by using the other Lifecycle methods such as onStart() or onResume
I'm not sure if it fits your needs, I had to do an update service that starts the first time I open ActivityA (main Activity) and stops when exiting from ActivityA (not returning back from ActivityB),
I've placed the "start code" in onCreate() when savedInstanceState is null and the "stop code" in onDestroy() if isFinishing() is true