Change app language programmatically in Android

前端 未结 30 2998
面向向阳花
面向向阳花 2020-11-21 04:34

Is it possible to change the language of an app programmatically while still using Android resources?

If not, is it possible to request a resource in an specific lan

30条回答
  •  梦谈多话
    2020-11-21 05:22

    The only solution that fully works for me is a combination of Alex Volovoy's code with application restart mechanism:

    void restartApplication() {
        Intent i = new Intent(MainTabActivity.context, MagicAppRestart.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        MainTabActivity.context.startActivity(i);
    }
    
    
    /** This activity shows nothing; instead, it restarts the android process */
    public class MagicAppRestart extends Activity {
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            finish();
        }
    
        protected void onResume() {
            super.onResume();
            startActivityForResult(new Intent(this, MainTabActivity.class), 0);         
        }
    }
    

提交回复
热议问题