[英]Is there a way to restart the application instead of having it restart the last activity after a crash?
[英]android: restart application instead of activity on language change
我的應用程序中確實有多個活動和多個片段。 每當我從設備語言設置更改語言環境時,只有當前活動會重新啟動。 在設備設置下更改語言后,是否可以重新啟動整個應用程序?
您可以注冊BroadcastReceiver
來偵聽意圖ACTION_LOCALE_CHANGED
,然后在收到意圖后,您可以執行重新啟動應用程序的邏輯。 像這樣:
BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
//RESTART APPLICATION
}
};
//register broadcastreceiver
LocalBroadcastManager.getInstance(getContext())
.registerReceiver(mBroadcastReceiver, new IntentFilter(Intent.ACTION_LOCALE_CHANGED));
//remember to unregister in onDestroy with LocalBroadcastManager.getInstance(mContext).unregisterReceiver(mBroadcastReceiver);
要“重新啟動”應用程序,可以使用:
Intent intent = new Intent(this, YourHomeActivity.class);
this.startActivity(intent);
this.finishAffinity();
開始家庭活動並清除所有其他活動。
或類似這樣的東西:
Intent mStartActivity = new Intent(MainActivity.this, MainActivity.class);
int mPendingIntentId = 123456;
PendingIntent mPendingIntent = PendingIntent.getActivity(MainActivity.this, mPendingIntentId, mStartActivity, PendingIntent.FLAG_CANCEL_CURRENT);
AlarmManager mgr = (AlarmManager)MainActivity.this.getSystemService(Context.ALARM_SERVICE);
mgr.set(AlarmManager.RTC, System.currentTimeMillis() + 100, mPendingIntent);
System.exit(0);
實際停止應用程序並稍后重新啟動100ms(將其設置為所需的值)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.