Android 切换系统语言后,重启App

问题描述:App->改变系统语言->重进App后,最近的Activity会走onCreate()方法,然后App被杀掉;直到第二次进入App后,App会重启
解决方案:App->改变系统语言->重进App后,最近的Activity走到onCreate()方法时,检测系统语言是否改变,如果是,就重启App。
LanguageUtil.class
package com.barry.common.util;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.TextUtils;

import java.util.Locale;

/**
 * 切换语言的工具类
 *
 * @author : barry.huang
 * @time : 16/9/21
 **/
public class LanguageUtil {

    private static final String LAST_LANGUAGE = "lastLanguage";

    /**
     * 当改变系统语言时,重启App
     *
     * @param activity
     * @param homeActivityCls 主activity
     * @return
     */
    public static boolean isLanguageChanged(Activity activity, Class<?> homeActivityCls) {
        Locale locale = Locale.getDefault();
        if (locale == null) {
            return false;
        }
        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(activity);
        String localeStr = sp.getString(LAST_LANGUAGE, "");
        String curLocaleStr = getLocaleString(locale);
        if (TextUtils.isEmpty(localeStr)) {
            sp.edit().putString(LAST_LANGUAGE, curLocaleStr).commit();
            return false;
        } else {
            if (localeStr.equals(curLocaleStr)) {
                return false;
            } else {
                sp.edit().putString(LAST_LANGUAGE, curLocaleStr).commit();
                restartApp(activity, homeActivityCls);
                return true;
            }
        }
    }

    private static String getLocaleString(Locale locale) {
        if (locale == null) {
            return "";
        } else {
            return locale.getCountry() + locale.getLanguage();
        }
    }

    public static void restartApp(Activity activity, Class<?> homeClass) {
        Intent intent = new Intent(activity, homeClass);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
        activity.startActivity(intent);
        // 杀掉进程
        android.os.Process.killProcess(android.os.Process.myPid());
        System.exit(0);
    }

}
在基类BaseActivity的onCreate()方法里调用LanguageUtil.isLanguageChanged(activity, homeActivityCls).
添加权限
<uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES" />
over.
Android 中,你可以使用 `Resources.updateConfiguration()` 方法来动态切换应用程序的语言环境,而无需重启应用程序。 以下是一个示例代码,用于在不重启应用程序的情况下切换应用程序的语言环境: ```java public void changeLanguage(Context context, Locale locale) { Resources res = context.getResources(); Configuration config = res.getConfiguration(); config.setLocale(locale); res.updateConfiguration(config, res.getDisplayMetrics()); } ``` 在这个示例中,`changeLanguage()` 方法接受一个 `Context` 对象和一个 `Locale` 对象作为参数。`Locale` 对象表示应该切换到的新语言环境。 该方法首先获取应用程序的 `Resources` 对象,然后获取当前的 `Configuration` 对象。接着,它将新的 `Locale` 对象设置为 `Configuration` 对象的语言环境,并使用 `Resources.updateConfiguration()` 方法更新应用程序的配置。 请注意,这个方法只会影响当前的 `Activity` 和后续创建的 `Activity`。如果你想要更新整个应用程序的语言环境,你需要在每个 `Activity` 中都调用这个方法。 另外,你需要在应用程序的 `AndroidManifest.xml` 文件中声明支持的语言环境,例如: ```xml <application ... android:supportsRtl="true" android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" android:configChanges="locale"> <activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="locale|orientation|screenSize" android:windowSoftInputMode="adjustPan"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".SecondActivity" android:label="@string/title_activity_second" android:parentActivityName=".MainActivity"> <meta-data android:name="android.support.PARENT_ACTIVITY" android:value=".MainActivity" /> </activity> </application> <supports-screens android:smallScreens="true" android:normalScreens="true" android:largeScreens="true" android:xlargeScreens="true" android:anyDensity="true" /> <uses-permission android:name="android.permission.INTERNET" /> ``` 在这个示例中,我们将 `android:configChanges` 属性设置为 `locale`,这样当用户切换语言时,我们就能够捕获 `Activity` 的 `onConfigurationChanged()` 方法,从而不必重新创建 `Activity`。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值