szaske
szaske

Reputation: 2027

How to get Application Lifecycle events in Activities?

I'm trying to create an app that can pop up a dialog any time the app returns from the background.

My app has multiple Activities and I've been trying to create a base class that checks for OnResume in the Activity. The problem is, when I switch between Activities, they fire OnResume which is not what I want, I only want the dialog to appear when the app itself resumes from the background.

I see Jetpack has Application lifecycle events which looks like exactly what I'm looking for in ProcessLifecycleOwner.

I'm trying code like this in my base activity, but now, apparently because I've loaded multiple activities my events are firing multiple times...once for each Activity/Listener.

How can I implement this so that there is only one listener but all Activities have access to the application events? I considered using the Application class as the single listener, but I'm not sure how I can then link into those events or pass that information back to my various Activities.

Upvotes: 0

Views: 1085

Answers (2)

Bö macht Blau
Bö macht Blau

Reputation: 13019

If you use the Application class to listen to lifecyle events, BaseActivity can register with the Application class as current Activity in onResume()

@Override
protected void onResume() {
    super.onResume();
    ((App)getApplication()).registerActivity(this);
}

The Application class needs fields

private boolean showDialog = false;
private BaseActivity currentRegisteredActivity = null;

then as soon as an Activity is registered it will be told to show the dialog if the app has been in the background up to now:

public void registerActivity(BaseActivity activity) {
    currentRegisteredActivity = activity;
    if(showDialog){
        activity.showInitialDialog();
        showDialog = false;
    }
}

The methods for observing the lifecycle events:

@OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
public void appInResumedState(){
    showDialog = true;
    Log.d(LIFECYLE_EVENT, "appInResumedState: ");
    if(currentRegisteredActivity != null){
        currentRegisteredActivity.showInitialDialog();
        showDialog = false;
    }
}

@OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
public void appInPausedState(){
    showDialog = false;
    Log.d("LIFECYLE_EVENT", "appInPausedState: ");
    currentRegisteredActivity = null;
}

Upvotes: 1

Jacob Collins
Jacob Collins

Reputation: 464

Maybe try something like this?

Have your activities (or fragments) implement a common interface that your Application will keep a reference to and call on during lifecycle events.

class MyApplication : Application(), LifeCycleObserver {
  private var delegate: LifeCycleDelegate? = null

  // ... Initialization code for LifeCycle

  fun updateCurrentDelegate(delegate: LifeCycleDelegate) {
   currentActivity = delegate
  }

  @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
  fun appInResumeState() {
      delegate.onResumeCalled()
  }
}

Knowing when to set the variable for the current delegate might be tricky.

This is all just guessing, though. I've only messed with LifeCycleOwner and LifeCycleObserver from an Activity so far. I hope this helps!

Upvotes: 0

Related Questions