设置语言改变时,主activity 会重新启动

语言变化后,当不设置android:configChanges这项时,会执行:onPause -> onStop -> onDestory -> onCreate -> onStart -> onResume。

如果设置了 android:configChanges="orientation|keyboard|keyboardHidden|locale"

将执行

@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}


当然这样将会导致语言变化后,Launcher的语言并没有改变,这并不符合项目需求。

这时可以在ondestroy方法中设置:

if(Build.VERSION.SDK_INT<=7){
Log.e(TAG, "destroy"+Build.VERSION.SDK_INT);
Intent intent = new Intent(getApplicationContext(), Launcher.class);
           PendingIntent sender = PendingIntent.getBroadcast(getApplicationContext(),0, intent, 0);


           // We want the alarm to go off 30 seconds from now.
           Calendar calendar = Calendar.getInstance();
           calendar.setTimeInMillis(System.currentTimeMillis());
           calendar.add(Calendar.SECOND, 1);


           // Schedule the alarm!
           AlarmManager am = (AlarmManager)getSystemService(ALARM_SERVICE);
           am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
  ActivityManager acm = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
       acm.restartPackage("com.android.launcher");
}else{
                     android.os.Process.killProcess(android.os.Process.myPid());
}

这样就会KIll掉整个进程。

你可能感兴趣的:(launcher学习)