Created
July 29, 2019 18:25
-
-
Save ZhangMenghe/442c2f073348f58b829480bd33339fc9 to your computer and use it in GitHub Desktop.
[Code snippet][Android] Restart Application
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //M1: exit and re-enter the application | |
| Intent mStartActivity = new Intent(this, MainActivity.class); | |
| int mPendingIntentId = 123456; | |
| PendingIntent mPendingIntent = PendingIntent.getActivity(this, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT); | |
| AlarmManager mgr = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE); | |
| mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent); | |
| System.exit(0); | |
| //M2: let the current activity finish, app itself won't close | |
| Intent i = new Intent(this, MainActivity.class); | |
| i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); | |
| i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); | |
| this.startActivity(i); | |
| this.finish(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment