Skip to content

Instantly share code, notes, and snippets.

@ZhangMenghe
Created July 29, 2019 18:25
Show Gist options
  • Select an option

  • Save ZhangMenghe/442c2f073348f58b829480bd33339fc9 to your computer and use it in GitHub Desktop.

Select an option

Save ZhangMenghe/442c2f073348f58b829480bd33339fc9 to your computer and use it in GitHub Desktop.
[Code snippet][Android] Restart Application
//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