Reputation: 6065
If I want to run some scripts before any activity starts and after all activity stops, Where would I put such a listener in android?
Ive considered putting it in the Android Application
class, and simply put the methods inside its onCreate
and onSaveInstanceState
- but I am not sure they get called before and after all activites
Upvotes: 1
Views: 881
Reputation: 1448
Unfortunately in the Application class there is no callback when the application has stopped, but I think you can try this code:
<< here was old code >>
Updated: Code of Application class:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
startService(new Intent(this, Monitor.class));
}
}
The code of background service:
public class Monitor extends Service implements Application.ActivityLifecycleCallbacks {
private static final String TAG = "Monitor";
int mActivitiesCount = 1; // At the moment when service started, the main Activity is already opened
public Monitor() {
}
@Override
public void onCreate() {
super.onCreate();
getApplication().registerActivityLifecycleCallbacks(this);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
return START_STICKY;
}
@Override
public void onDestroy() {
getApplication().unregisterActivityLifecycleCallbacks(this);
super.onDestroy();
}
@Override
public void onActivityCreated(Activity activity, Bundle bundle) {
mActivitiesCount++;
Log.d(TAG, "onActivityCreated: count: " + mActivitiesCount);
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {
}
@Override
public void onActivityDestroyed(Activity activity) {
mActivitiesCount--;
Log.d(TAG, "onActivityDestroyed: count: " + mActivitiesCount);
if (mActivitiesCount == 0) {
Log.d(TAG, "All activities was destroyed");
// do something
}
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
Upvotes: 2