Reputation: 1179
I have to run some background tasks whenever an application is activated / or starts running. Suppose u r running the application and then suddenly the device got off or any how u got out of the application without stopping it properly, then when you will again start the application, obviously it will start running from the previous state before getting out of the application. When it starts again or activated, i want my background tasks to be done.Is there any function which is called every time an application starts which i can use to initialize those background tasks? If not then how can i accomplish my purpose? Need help on this , Thanks in advance .... !!!
Upvotes: 2
Views: 434
Reputation: 4877
Well - there's no such facility available in the Android SDK for you to determine whether or not your application is running - this is primarily because of the way applications are handled by Android.
For example - what would you mean by saying the application is not running? would that mean the UI s not visible, or the process related to the application is not running? Btw, I hope that you know that:
Application's lifetime != Application process' lifetime.
As for running background tasks, you can either make use of AsyncTask
or the Service
classes.
And if you're concerned about only the Application's visible lifetime, the you'll have to rely on making use of the onPause
and onResume
methods, to check on flags which you can turn on/off to capture the state of the app.
Upvotes: 0
Reputation: 29176
Yes, the main activity class's OnCreate
method is called whenever an application is started.
From the documentation -
Called when the activity is starting. This is where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById(int) to programmatically interact with widgets in the UI, calling managedQuery(android.net.Uri, String[], String, String[], String) to retrieve cursors for data being displayed, etc.
This question might be helpful. From the accepted answer -
OnCreate is called regardless of you start the app or you resume the app.
Upvotes: 0
Reputation: 55744
Whenever an application returns from being in the background, at very least it will run the the onResume()
function:
@Override
public void onResume(){
//You have to call super.onResume(), otherwise an exception is thrown
super.onResume();
// and then do whatever you want here.
}
Upvotes: 1