Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restart app programmatically? [duplicate]

Tags:

android

I need to restart the app programmatically. My launcher activity is called 'Login' and after login, the main activity is called 'Main'. From within the main activity I want to restart the app. So I have the following:

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

This will show the 'Login' activity, however when I press back I'm returned back to the previous activity.

Is there a better way to really restart the app?

like image 887
Ivan-Mark Debono Avatar asked Dec 03 '15 11:12

Ivan-Mark Debono


2 Answers

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();
like image 121
johnrao07 Avatar answered Oct 30 '22 09:10

johnrao07


Try this

call finish()

Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
like image 36
N J Avatar answered Oct 30 '22 11:10

N J