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)
How to restart an app programmatically for android?
If you want restart app programmatically,
This code work fine for me and you can set time elapse before restart app
uses
Androidapi.Helpers,Androidapi.JNI.GraphicsContentViewText,Androidapi.JNI.App,
System.DateUtils;
...
procedure RestartApp;
{$IFDEF ANDROID}
var LPM : JPackageManager;
LIntent_Start : JIntent;
LPendingIntent : JPendingIntent;
LMS : Int64;
{$ENDIF}
begin
{$IFDEF ANDROID}
LPM := TAndroidHelper.Context.getPackageManager();
LIntent_Start := LPM.getLaunchIntentForPackage(
TAndroidHelper.Context.getPackageName()
);
LIntent_Start.addFlags( TJIntent.JavaClass.FLAG_ACTIVITY_CLEAR_TOP );
LPendingIntent := TJPendingIntent.JavaClass.getActivity(
TAndroidHelper.Context,
223344 {RequestCode},
LIntent_Start,
TJPendingIntent.JavaClass.FLAG_CANCEL_CURRENT
);
LMS := DateTimeToUnix( Now, False {InputIsUTC} ) * 1000;
TAndroidHelper.AlarmManager.&set(
TJAlarmManager.JavaClass.RTC,
LMS + 10000,
LPendingIntent
);
// TAndroidHelper.Activity.finish();
Application.Terminate;
{$ENDIF }
end;
Related Topics
How to Use Scrollview in Android
How to Use Asynctask Correctly in Android
How to Sign Android App With System Signature
How to Send String from One Activity to Another
How to Animate Recyclerview Items When They Appear
How to Install Trusted Ca Certificate on Android Device
Notification Icon with the New Firebase Cloud Messaging System
Android Static Object Lifecycle
How to Change Language of App When User Selects Language
Set Transparent Background of an Imageview on Android
Android.Content.Activitynotfoundexception:
How to Find Serial Number of Android Device
How to Get Current Time from Internet in Android
How to Align Views at the Bottom of the Screen
Set Android Shape Color Programmatically
Deleting a Gallery Image After Camera Intent Photo Taken
List of Android Permissions Normal Permissions and Dangerous Permissions in API 23