Android中重启应用的几种方法

方法一:

Intent intent = new Intent(LoginActivity.this, LoginActivity.class);
startActivity(intent);
System.exit(0);
finish();

说明: 其中,LoginActivity为重启后的界面,点击重启功能之后,应用是不会走onDestroy()方法,直接走onCreate()–>onResume()。

方法二:

Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
//与正常页面跳转一样可传递序列化数据,在Launch页面内获得
intent.putExtra("REBOOT","reboot");
PendingIntent restartIntent = PendingIntent.getActivity(getApplicationContext(), 0, intent, PendingIntent.FLAG_ONE_SHOT);
AlarmManager mgr = (AlarmManager)getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, restartIntent);
android.os.Process.killProcess(android.os.Process.myPid());

说明:点击重启功能之后,应用是不会走onDestroy()方法,直接走onCreate()–>onResume()。

方法三:

Intent intent = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//与正常页面跳转一样可传递序列化数据, 在Launch页面内获得
intent.putExtra("REBOOT","reboot");
startActivity(intent);

说明:点击重启功能之后,应用是会走启动初始界面onCreate()–>onResume(),之后走最后activity的onDestory()方法。

你可能感兴趣的:(Android,android)