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.
How to programmatically restart an app in Android 10?
Update: To restart your application, Make your Root Activity's Intent, while setting these flags
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
This will ensure a fresh instance of your root activity is launched (the first flag), while all previous activities are popped off the stack (the second flag)
Use recreate()
when inside the same activity.
If your app is not running, it's already refreshed on opening anyways.
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();
Completely restart android app including application class
This restarts the whole application
val intent = Intent(context, MainActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
context.finish()
Runtime.getRuntime().exit(0)
Related Topics
Android Emulator: How to Monitor Network Traffic
Difference Between Android-Support-V7-Appcompat and Android-Support-V4
Low-Latency Audio Playback on Android
Why Was "Avoid Enums Where You Only Need Ints" Removed from Android's Performance Tips
How to Display Input Errors in Popup
How to Create Custom UI for Android Mediacontroller
Navigation Drawer Semi-Transparent Over Status Bar Not Working
Android Persistence Room: "Cannot Figure Out How to Read This Field from a Cursor"
Jni and Gradle in Android Studio
How to Hide a Button Programmatically
How to Add a Fast-Scroller to the Recyclerview
Android - Acceleration Down (Smash)
Progressbar Togther with Asynctask
How to Attach Multiple Files to Email Client in Android
Passing Strings Between Activities in Android