Force application to restart on first activity
Here is an example to restart your app in a generic way by using the PackageManager:
Intent i = getBaseContext().getPackageManager()
.getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);
Restarting Android app programmatically
If you just consider to switch to your starting Activity
, refer to Ricardo's answer. But this approach won't reset static context of your app and won't rebuild the Application
class, so the app won't be really restarted.
If you want to completely restart your app, I can advise more radical way, using PendingIntent
.
private void restartApp() {
Intent intent = new Intent(getApplicationContext(), YourStarterActivity.class);
int mPendingIntentId = MAGICAL_NUMBER;
PendingIntent mPendingIntent = PendingIntent.getActivity(getApplicationContext(), mPendingIntentId, intent, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager) getApplicationContext().getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);
}
P.S. Tried your code in my project - works well with and without finish()
. So maybe you have something specific about your Activity or Fragment, you haven't written.
Android: How to auto-restart an application after it has been force closed ?
To accomplish this you have to do two things:
- Avoid the "Force close" - standard way of application crash.
- Setup a restart mechanism when the crash happens anyway.
See below how to do these:
Call
Thread.setDefaultUncaughtExceptionHandler()
in order to catch all uncaught exception, in which caseuncaughtException()
method will be called. "Force close" will not appear and the application will be unresponsive, which is not a quite good thing.
In order to restart your application when it crashed you should do the following :In the
onCreate
method, in your main activity initialize aPendingIntent
member:Intent intent = PendingIntent.getActivity(
YourApplication.getInstance().getBaseContext(),
0,
new Intent(getIntent()),
getIntent().getFlags());
Then put the following in your uncaughtException()
method:
AlarmManager mgr = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 2000, intent);
System.exit(2);
You also must call System.exit()
, otherwise will not work.
In this way your application will restart after 2 seconds.
Eventually you can set some flag in your intent that the application crashed and in your onCreate()
method you can show a dialog "I'm sorry, the application crashed, hope never again :)".
Restart app programmatically?
Try below code
Intent i = getBaseContext().getPackageManager().
getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
And another thing before calling your second Activity call
finish();
Related Topics
How to Increase Storage for Android Emulator? (Install_Failed_Insufficient_Storage)
Android Singletask or Singleinstance Launch Mode
How to Access Call Log for Android
How to Prevent an Android Device from Going to Sleep Programmatically
Android. How Does Notifydatasetchanged() Method and Listviews Work
What Does Google-Services.JSON Really Do
How to Connect Android App to MySQL Database
Android: Remove Notification from Notification Bar
Webview Load HTML from Assets Directory
Com.Google.Android.Gms:Play-Services-Measurement-Base Is Being Requested by Various Other Libraries
Cannot Get Searchview in Actionbar to Work
How to Change the Radio Button Icon in an Android Radio Button Group
Android Preventing Double Click on a Button
Android Linearlayout:Add Border with Shadow Around a Linearlayout
Finish an Activity from Another Activity