How to Programmatically "Restart" an Android App

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



Leave a reply



Submit