Erik Sapir
Erik Sapir

Reputation: 24717

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?

Upvotes: 15

Views: 12472

Answers (3)

Monty
Monty

Reputation: 3215

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);

Upvotes: 1

Jeshurun
Jeshurun

Reputation: 23186

You should probably be looking at storing such Objects in your app's implementation of the Application class.

If these objects contain state that needs to be more persistent, you should save the state of such Objects in each Activity's onPause() method, either to the database, in SharedPreferences or remotely.

Upvotes: -1

Ovidiu Latcu
Ovidiu Latcu

Reputation: 72311

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);

Upvotes: 9

Related Questions