Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restarting Android application after process is killed

When my application is idle, Android kills the process. If user reopens the application after some time, only the top Activity is created - this is a problem for me because the activity depends on initialization of other objects (which are now destroyed).

What I want to do in that case is to re-launch the application. How can I do that?

like image 582
Erik Sapir Avatar asked Jul 24 '12 07:07

Erik Sapir


People also ask

How do I automatically restart apps on Android?

Reboot Manager / Widget is a combination widget and application for Android devices. It lets you schedule a fast reboot, full boot, hot reboot, recovery or full shutdown via a simple graphical user interface. You can also set up nightly reboots of your smartphone or tablet.

How do you run the service even after killing the application in Android?

First, the easiest way to do what you're trying to do is to launch an Android Broadcast when the app is killed manually, and define a custom BroadcastReceiver to trigger a service restart after that. Dear Dr Sabri Allani, If your Service is started by your app then actually your service is running on main process.

How do I restart a program after crash?

In order to restart your application when it crashed you should do the following : In the onCreate method, in your main activity initialize a PendingIntent member: Intent intent = PendingIntent. getActivity( YourApplication.

Is onDestroy called when app is killed?

Android app doesn't call "onDestroy()" when killed (ICS)


2 Answers

Just identify that your Application is being launched after it was previously destroyed by Android, you could do this by keeping a variable in a custom Application class, and set it to true after your applicaiton is initialized. So when the applicaction is re-launched, this flag is false, and then just make an Intent to launch your main Activity specifying FLAG_ACTIVITY_CLEAR_TOP :

Intent reLaunchMain=new Intent(this,MainActivity.class);
reLaunchMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(reLaunchMain);
like image 185
Ovidiu Latcu Avatar answered Sep 27 '22 23:09

Ovidiu Latcu


I think this answer only for you.

After finish progress call this

        finish();
        Intent intent = new Intent(this, sameactivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
like image 37
Monty Avatar answered Sep 28 '22 01:09

Monty