I do have mutiple actvities and multiple fragments running in my application. Whenever i change the locale from the device language settings, only current activity gets restarted. Is there a way to restart the whole application on change in language under device setting ?
You can register a BroadcastReceiver
to listen for the intent ACTION_LOCALE_CHANGED
, then upon receiving the intent you can do your logic of restarting the application. Like this:
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);
To "restart" the application, you can use:
Intent intent = new Intent(this, YourHomeActivity.class);
this.startActivity(intent);
this.finishAffinity();
to start your home activity and clear all other activities.
Or something like this:
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);
to actually stop the application and restart 100ms (set it to whatever you want) later.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.