设置语言改变时,主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掉整个进程。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值