Android: application global onPause() and onResume()?

Is there something like an application global onPause() and onResume()?

My main activity listens for GPS fixes, which I want to continue working when switching to another screen/activity. Therefor I cannot unregister my LocationListener in the activity's onPause(). However I still want to unregister my GPS listener when switching to another application (so save battery) and turning it back on when returning to my application, regardless what screen/activity the user is currently in.

Any ideas?

Answers


Is there something like an application global onPause() and onResume()?

No, sorry.

My main activity listens for GPS fixes, which I want to continue working when switching to another screen/activity. Therefor I cannot unregister my LocationListener in the activity's onPause(). However I still want to unregister my GPS listener when switching to another application (so save battery) and turning it back on when returning to my application, regardless what screen/activity the user is currently in.

Here's one possible approach:

Step #1: Move the LocationListener logic into a Service, which the activities connect to via the local binding pattern or something. Also have at least one service call startService(), so an unbindService() won't cause the Service to go away (assuming you're using the local binding pattern).

Step #2: Have the activities call into the service during onPause() and onResume().

Step #3: Have the service maintain a reference count of outstanding activities.

Step #4: When the reference count reaches zero, have the service arrange to get woken up via a Timer and TimerTask. Also, cancel any such outstanding TimerTask if the reference count gets incremented.

Step #5: Have the TimerTask shut down GPS if it ever gets executed.

The net is that you will only release GPS after such-and-so amount of inactivity. That inactivity could be for any reason.


You Can Create A Application Class and if its implements Application.ActivityLifecycleCallbacks

public class AppController extends Application implements Application.ActivityLifecycleCallbacks
{

    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        Toast.makeText(this, "----------onCreate()---------", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onActivityStarted(Activity activity) {
        Log.i("ApplicationName","----------onActivityStarted()---------");
    }

    @Override
    public void onActivityResumed(Activity activity) {
        Log.i("ApplicationName","----------onActivityResumed()---------");
    }

    @Override
    public void onActivityPaused(Activity activity) {
        Toast.makeText(this, "----------onActivityPaused()---------", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onActivityStopped(Activity activity) {
        Log.i("ApplicationName","----------onActivityStopped()---------");
    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
        Log.i("ApplicationName","----------onActivitySaveInstanceState()---------");
    }

    @Override
    public void onActivityDestroyed(Activity activity) {

    }
}

Then Call from Your activity

@Override
protected void onPause() {
    AppController controller=(AppController)getApplicationContext();
    controller.onActivityPaused(Main3Activity.this);
    super.onPause();
}

Need Your Help

PLY: Token shifting problem in C parser

python parsing yacc ply

I'm writing a C parser using PLY, and recently ran into a problem.

How to validate that username does not start with certain string?

ruby-on-rails ruby regex validation ruby-on-rails-4

One of the variables in a User model is username. I would like to reserve usernames starting with "vondel" for something else. Therefore, I would like to make it invalid for a username to start with "