Reputation: 2494
I have an android app that needs to run an async task every time it starts up... Not every time I open an activity but every time the app starts up. I don't think the activity life-cycle will help me here so I'm looking for suggestions.
Upvotes: 1
Views: 1078
Reputation: 1899
Mark your start activity as main in your Manifest like this
<activity
android:name="com.your.package.name.activityName"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Add a boolean extra to every intent that his target be the main start activity. When the main activity starts if the intent not has the boolen extra then create the async task. If exist the boolean extra means that have parent and not is a start of app.
Here have more info about Async Task
http://developer.android.com/reference/android/os/AsyncTask.html
Upvotes: 0
Reputation: 3031
Make a subclass of Application, override its onCreate()
and then tell Android to use your subclass by setting its full classname as the value of 'android:name' in <application />
in your Android manifest.
<application
android:name="com.yourpackage.YourApplicationSubclass"
....
/>
Now with more detail:
android.app.Application
is the base class that represents your application. While you generally don't need to subclass Application, doing so gives you the opportunity to get your hooks into your application's lifecycle, rather than just your Activities', Intents', and Services'.
All you need to do is create a new subclass of Application:
import android.app.Application;
public class MyApplication extends Application
{
@Override
public void onCreate() {
// Do stuff when the application starts
}
}
And then updated your AndroidManifest.xml as I described above.
Upvotes: 2