简体   繁体   English

如何在设置更改时重新启动我的Android应用程序?

[英]How to restart my android application on settings change?

I am developing a bilingual application. 我正在开发双语应用程序。 I want it to get fully restarted on language settings change. 我希望它在语言设置更改时完全重新启动。 Here is the code I am using: 这是我正在使用的代码:

Intent restartIntent = getBaseContext().getPackageManager()
                            .getLaunchIntentForPackage(getBaseContext().getPackageName() );
    PendingIntent intent = PendingIntent.getActivity(getBaseContext(), 0, restartIntent,     Intent.FLAG_ACTIVITY_CLEAR_TOP);
    AlarmManager manager = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
    manager.set(AlarmManager.RTC, System.currentTimeMillis() + 1, intent);
    System.exit(2);

This works fine except that after the app is restarted, I have two main activities (the old one still remains). 正常运行,除了重新启动应用程序后,我有两个主要活动(旧的活动仍然存在)。 How can I solve this issue? 我该如何解决这个问题?

Add finish(); 添加finish(); to your current Activity 您当前的活动

Intent restartIntent = getBaseContext().getPackageManager()
                            .getLaunchIntentForPackage(getBaseContext().getPackageName() );
    PendingIntent intent = PendingIntent.getActivity(getBaseContext(), 0, restartIntent,     Intent.FLAG_ACTIVITY_CLEAR_TOP);
    AlarmManager manager = (AlarmManager) getBaseContext().getSystemService(Context.ALARM_SERVICE);
    manager.set(AlarmManager.RTC, System.currentTimeMillis() + 1, intent);
    finish();
    System.exit(2);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM