Home » Android » Restarting Android app programmatically

Restarting Android app programmatically

Posted by: admin June 15, 2020 Leave a comment

Questions:

This is a follow up question to this question:

Force application to restart on first activity

I am trying to restart my application from a fragment like that:

    Toast.makeText(getActivity(), "Restarting app", Toast.LENGTH_SHORT).show();
    Intent i = getActivity().getBaseContext().getPackageManager()
.getLaunchIntentForPackage(getActivity().getBaseContext().getPackageName() );
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(i);
    getActivity().finish();

The code does nothing. The finish() is the only thing working for some reason. If I remove the finish(), nothing happens. Why is that?

How to&Answers:

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.

Answer:

Please refer below code

    Intent intent = new Intent(this, YourHomeActivity.class);
    this.startActivity(intent);
    this.finishAffinity();

It is starting your home activity and dismiss all other activities. It looks like a restart to users, but the process is the same.

Answer:

try this one

 Intent intent = getActivity().getBaseContext().getPackageManager().getLaunchIntentForPackage(getActivity().getBaseContext().getPackageName() );
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    startActivity(intent);
                    android.os.Process.killProcess(android.os.Process.myPid());
                    System.exit(0);

Answer:

Tray this

getActivity().finish();
getActivity().startActivity(new Intent(getContext(), Main.class));
getActivity().finishAffinity();

where finishAffinity() close all other Intents

Answer:

Once you add this FLAGSto the intent

i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

and call startActivity(), it will clear all the other activities including the one you call this from, so calling finish() after startActivity() will close the started activity.

Basically, remove getActivity().finish().

Answer:

As the accepted answer doesn’t seem to work on Android Q anymore due to the new background launch limitations, let me add my own solution:

startActivity(
    Intent(applicationContext, MainActivity::class.java)
)
System.exit(0)

Although it looks strange, it works very reliably for me, as the intent takes longer to fire than the system exit does. Plus, it is fired from the main thread, so Android Q won’t complain.

Answer:

Intent i = getBaseContext().getPackageManager()
             .getLaunchIntentForPackage( getBaseContext().getPackageName() );
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(i);

You can refer to previous discussion (click here)

Answer:

try using below lines:

Intent i = getBaseContext().getPackageManager()
                .getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
ActivityCompat.finishAfterTransition(YourCurrentActivity.this);

ActivityCompat.finishAfterTransition(YourCurrentActivity.this); this line clear the instance after creating another activity

Answer:

Add this code as per your activity and your app will restart. This code is in kotlin.

val intent = Intent(baseContext, MainActivity::class.java)
val pendingIntentId = 101
val pendingIntent = PendingIntent.getActivity(this, pendingIntentId,intent, PendingIntent.FLAG_CANCEL_CURRENT)
val alarmManager = (this.getSystemService(Context.ALARM_SERVICE)) as AlarmManager
alarmManager.set(AlarmManager.RTC, System.currentTimeMillis() + 100, pendingIntent)
exitProcess(0)