Sherif El Nady
Sherif El Nady

Reputation: 527

Restart a single task activity

I have a singleTask activity and my app supportsRtl, so when I try to change the language, this SingleTask activity is not affected so I need to restart it, Any help?

Upvotes: 3

Views: 928

Answers (2)

Sandeep Sharma
Sandeep Sharma

Reputation: 1885

If we use this approach we don't need to restart the activity

change your app language with the following code:

public static void notifyForLanguageChange(Context context, String languageCode) {
        Resources res = context.getResources();
        // Change locale settings in the app.
        DisplayMetrics dm = res.getDisplayMetrics();
        android.content.res.Configuration conf = res.getConfiguration();
        conf.locale = new Locale(languageCode.toLowerCase());
        res.updateConfiguration(conf, dm);
    }

and after that change your screen labels with(i.e reload string resources)

public void setLabels() {
        txtFirstView.setText(R.string.first);
        txtSecondView.setText(R.string.second);
        btnThirdView.setText(R.string.third);
    }

Upvotes: 0

Nir Duan
Nir Duan

Reputation: 6392

If you're using API 11 and above you can use the:

Activity.recreate()

If you need to support lower API use this to call your activity again:

Intent i = getIntent();
finish();
startActivity(i);

Upvotes: 6

Related Questions