Search code examples
androidandroid-activityandroid-instrumentation

Android - What is the best way to intercept the intent fired to launch into an activity after being killed by the LMK?


I'm working on an Android app and I'd like to run some code during app launch, specifically in the case that we launch directly into an activity.

The problem we're seeing appears to be happening when the LMK kills our app's process; when a user re-launches the app, Android sends us straight to the activity that was on the top of the stack, but the code I want to run doesn't run in this case, which results in crashes later.

The part that complicates things is that it is important to know which activity is being launched since that changes how our setup code runs.

I'm noob'ing my way around and trying to set up an ActivityMonitor (doc) but it's not firing. This code that actually initializes and adds this monitor to the instrumentation does execute, but the onStartActivity callback is never called.

I did the following:

Instrumentation instrumentation = getInstrumentation();
IntentFilter filter = new IntentFilter();
filter.addCategory(Intent.CATEGORY_LAUNCHER);

ActivityMonitor monitor =
    new Instrumentation.ActivityMonitor(filter, null, true) {
        @Override
        public Instrumentation.ActivityResult onStartActivity(Intent intent) {
            // Get name of activity to be launched
            // Run set up code
            startActivity(intent);
            return new Instrumentation.ActivityResult(0, null);
        }
    };

instrumentation.addMonitor(monitor);

Questions:

  1. What does this intent look like when the system is re-launching an activity after the process has been killed? Does it have a category of Intent.CATEGORY_LAUNCHER?
  2. Can IntentFilters work if they only have a category associated (ie, no activity class)?
  3. Is there anything else obviously wrong here? I'm not the greatest Android dev, so looking to learn something here.

Solution

  • What does this intent look like when the system is re-launching an activity after the process has been killed?

    It will be the same Intent that was used to start the activity in the first place.

    Does it have a category of Intent.CATEGORY_LAUNCHER?

    Only if the Intent that was used to start the activity in the first place had that category.

    Can IntentFilters work if they only have a category associated (ie, no activity class)?

    Not really.

    Is there anything else obviously wrong here?

    In terms of the code shown in the question, Instrumentation is used by testing, not by production apps.

    Android sends us straight to the activity that was on the top of the stack, but the code I want to run doesn't run in this case, which results in crashes later

    Then put that code in the activity. Or, set up your app to support lazy-initialization of this stuff.

    The part that complicates things is that it is important to know which activity is being launched since that changes how our setup code runs.

    Then put that setup code in the activity. Or, set up your app to support lazy-initialization of this stuff.

    You are welcome to use something like Application and onCreate() for quick initialization that should occur for whatever reason your process was created. However, if your activity is dependent on something specific to that activity, that logic belongs in that activity (or in classes utilized by that activity, such as a ViewModel).