I am developing an app which will be performing some background function within a JobIntentService.

The user initiates action from Screen A. A background Intent Service is started. After the Intent service finishes its work it sends a callback to the Screen A and then Screen Sartes Screen B which utilizes the results of Intent Service.

My issue is that suppose the user starts the Action and then minimizes the app and then the Android OS clears the Screen A due to running on low Memory and then the Service sends back the callback.

My question is Now since Screen A no longer exists will it receive the callback? Is it possible to figure out that Screen A has to be cleared by Android OS so that my service can directly start Screen B?

Also, how can such a scenario be handled?

Any suggestion would be grateful.

2 Answers

2
Sultan Mahmud On Best Solutions

The better approach is to create a standalone BroadcastReceiver. This insures that your app can respond to the broadcast, whether or not the Service is running. In this case you can check in service activity is still alive or not. then take decision on that.

Use the below method with your package name. It will return true if any of your activities is in foreground.

public boolean isForeground(String myPackage) {
    ActivityManager manager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
    List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1); 
    ComponentName componentInfo = runningTaskInfo.get(0).topActivity;
    return componentInfo.getPackageName().equals(myPackage);
}
1
Abu Yousuf On

Also, how can such a scenario be handled?

You can solve this by the importance of the background task.

If you think that your background task is important to notify the user then You can crate a Notification after your background work is done. So your user will get a update of your task.You can set result data to Intent and start your Screen B after notification click.

If its not important to notify the user the you can save background task result and when user comes back to your app your can send the user to Screen B.

Edit:

I think when your Activity is in background and is killed by System then starting an Activity from background service is not good UX.Because that time user may be doing some important task(game, online streaming etc) on other app. Which will give user a bad user experience.