I have declared a global boolean variable in my MainActivity class and set its value to true:
public class MainActivity extends AppCompatActivity {
public boolean inAppTextIntegration = true;
//onCreate method and stuff
I have 11 more activities in my application. And, in every activity, I'm overriding the attachBaseContext method as:
@Override
protected void attachBaseContext(Context base) {
if(inAppTextIntegration) {
super.attachBaseContext(ZeTarget.attachBaseContext(base, this));
}
else {
super.attachBaseContext(base);
}
}
I have declared the boolean variable inAppTextIntegration
in all my activities. Now the problem part: Since I'm doing the same thing in all the activities, is it possible to reduce the code?
Currently, I'm using SharedPreferences
to store the value of inAppTextIntegration
in my MainActivity, and then accessing that value in other activities because I don't want to set this value manually in all the activities.
Again, the problem that I face is in all other activities, else
part of the overriding method is being executed (I'm initializing inAppTextIntegration
variable in onCreate
method of all other activities. Doing it outside the onCreate method causes null pointer exception as the context is not set yet).
So, how do I control the override to attachBaseContext
at all other activities just by changing one variable in the MainActivity?