Nimesh Chandramaniya
Nimesh Chandramaniya

Reputation: 628

Don't kill app when removed from recent apps

I want my application to keep running when the user removes the application from the recent apps screen.

Current Scenario:

The activity stack: A --> B --> C (foreground service started from this activity). Now, if the user removes the application from the recent apps screen then the service keeps running but the activity A,B and C are killed. Now, if the user launch the application then the activity A is started.

Result required:

I want the application to start directly with activity C with the state before it was previously killed.

I would appreciate any suggestions and thoughts on this topic. Thank you.

Upvotes: 1

Views: 719

Answers (1)

Sourabh Bans
Sourabh Bans

Reputation: 3134

Here is the nearby solution you can try.. might be helpful. You can use the method of your application class, where you can track your current running activity. For opening the last activity maintain and save a variable in SharedPreferences on activity paused/or created(whatever work) in below method.

private void setupActivityListener() {
        registerActivityLifecycleCallbacks(new ActivityLifecycleCallbacks() {
            @Override
            public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
                if (activity instanceof MainActivity) {
                    Log.e(TAG, "**************** MainActivity Created *******************");

                }
                //Log.e(TAG, "**************** onActivityCreated *******************");
            }

            @Override
            public void onActivityStarted(Activity activity) {
                //Log.e(TAG, "**************** onActivityStarted *******************");
            }

            @Override
            public void onActivityResumed(Activity activity) {
                CancelNotification();
                activeActivity = activity;
                //Log.e(TAG, "**************** onActivityResumed *******************");
            }

            @Override
            public void onActivityPaused(Activity activity) {
                activeActivity = null;
                //Log.e(TAG, "**************** onActivityPaused *******************");
            }

            @Override
            public void onActivityStopped(Activity activity) {
                //Log.e(TAG, "**************** onActivityStopped *******************");
            }

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

            @Override
            public void onActivityDestroyed(Activity activity) {
                if (activity instanceof MainActivity) {
                    Log.e(TAG, "**************** MainActivity Destroyed *******************");

                }
            }
        });
    }

And when open the app, check at startup what was the last activity.. if it wasn't MainActivity then redirect to the desired activity...

For state you can save it to bundle or some other source.. like in sharedpreferance.

Upvotes: 1

Related Questions