Reputation: 344
Is there any way to get intent extras inside attachBaseContext()
method?
The Activity i am using is inside the framework project. I need to set Activity's language with attachBaseContext()
method like:
@Override
protected void attachBaseContext(Context newBase) {
super.attachBaseContext(LanguageContextWrapper.wrap(newBase, "en"));
}
I am sending language code string to Activity with intent.putExtra()
. When i try to get extras inside attachBaseContext()
, it throws NullPointerException error. How can it be done? Thanks.
Upvotes: 3
Views: 1484
Reputation: 891
TL;DR - To the best of my knowledge, this is impossible.
This is because Android calls attachBaseContext
as soon as it attaches the Activity
as can be seen here. Later, the intent is set inside the Activity
class.
An alternative solution would be to store the variables in your Application
subclass or in an application scoped repository of some kind.
For example,
@Override
protected void attachBaseContext(Context newBase) {
String someLanguage = ((MyApplication)newBase.getApplicationContext()).getAppLanguage();
super.attachBaseContext(LanguageContextWrapper.wrap(newBase, someLanguage));
}
You could easily adapt this to use a repository pattern with Dagger or even SharedPreferences.
Upvotes: 1