How to detect when the app goes to minimized or ex

2020-06-23 06:32发布

问题:

Hey I have an application where I want to now when the APP goes to onPause or onDestroy because I want to call a certain function when this happens. I tried to override the onPause in an activity and extended that activity in all project but the onPause was being called on every migration between activities (which is logical) but this is not what I want. I want to know when the user exits the app or pauses it (pressing the home button) Regards,

回答1:

Pull this dependency in your build.gradle file:

dependencies {
implementation "android.arch.lifecycle:extensions:1.1.1"
}

Then in your Application class, use this:

public class MyApplication extends Application implements LifecycleObserver {

@Override
public void onCreate() {
    super.onCreate();
    ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
private void onAppBackgrounded() {
    Log.d("MyApp", "App in background");
}

@OnLifecycleEvent(Lifecycle.Event.ON_START)
private void onAppForegrounded() {
    Log.d("MyApp", "App in foreground");
}
}

Update your AndroidManifest.xml file:

<application
    android:name=".MyApplication"
    ....>
</application>


回答2:

in all of your activities :

@Override
protected void onUserLeaveHint() {
    super.onUserLeaveHint();
    Log.e("TAG", "Activity Minimized");
}

in your main activity :

@Override
public void onBackPressed() {
    Log.e("TAG", "App Exit");
    super.onBackPressed();
}

note : there is no way to detect when app killed by system ( for example quit with task manager )



回答3:

I use this method to detect if the app goes to background or is killed

step 1: Make a service like this

public class OnClearFromRecentService extends Service {

@Override
public IBinder onBind(Intent intent) {
return null;

}

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("ClearFromRecentService", "Service Started");
return START_NOT_STICKY;

}

@Override
public void onDestroy() {
super.onDestroy();
Log.d("ClearFromRecentService", "Service Destroyed");

}

@Override
public void onTaskRemoved(Intent rootIntent) {
Log.e("ClearFromRecentService", "END");
//Code here
stopSelf();

} }

step 2)register this service in manifest.xml

<service android:name="com.example.OnClearFromRecentService" android:stopWithTask="false" />

step 3) Then start this service on your splash activity

startService(new Intent(getBaseContext(), OnClearFromRecentService.class));

So now when your app is manually killed, the onDestroy method will be called

Try this on your code and let me know if its working. Happy Coding :)



回答4:

If the user exits the application the onDestroy() callback will be called, if the user presses the home button the onStop() callback will be called.



回答5:

So you want to know when the whole Application goes into Bacground, the method from Activity.onPause tells you when a single abtivity goes into background.

For your needs you could add

to your android manifest. this will call a class like this with name "YourApplication"

public class YourApplication extends Application {
  @Override
  public void onCreate() {}
  public public void onTerminate() {
    super.onTerminate();
  }
}

e.g. for "onCreate" or "onTerminate" of the whole App (not for every single Activity). But java documentation say you should not trust that onTerminate is called on every app destroy (see: android.app.Application subclass, onTerminate is not being called).

I Think the best way is, if you

  • add a own Methode "onPause" and "onResume" to your Application class
  • in ALL your Activities you store into Application class when onPause or onResume is called
  • and you call you own Application.onPause when ALL of you activities are in "onPause"
  • and you call you Application.onResume when first Activity is back in "onResume"


回答6:

Implement

Application.ActivityLifecycleCallbacks

in your Application Class. It will implement the following ActivityLifeCycle Methods :

isActivityVisible()
onActivityCreated()
onActivityStarted()
onActivityResumed()
onActivityPaused()
onActivityStopped()
onActivitySaveInstanceState()
onActivityDestroyed()

And Register the callback listener in OnCreate() of application class like :

registerActivityLifecycleCallbacks(this);

Define a Global variable in application class.

private static boolean isActive;

Now change the activity life cycle's overridden method like this:

 public static boolean isActivityVisible() {
        return isActive;
    }

    @Override
    public void onActivityCreated(Activity activity, Bundle bundle) {

    }

    @Override
    public void onActivityStarted(Activity activity) {

    }

    @Override
    public void onActivityResumed(Activity activity) {
        isActive = true;
    }

    @Override
    public void onActivityPaused(Activity activity) {
        isActive = false;
    }

    @Override
    public void onActivityStopped(Activity activity) {

    }

    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle bundle) {

    }

    @Override
    public void onActivityDestroyed(Activity activity) {
            isActive = false;

    }

and you can perform the required operations on the value of isActive variable.



标签: android cycle