3

In my MainActivity class, I want to stop overriding the attachBaseContext method if a button is pressed in my view.

Here's the situation:

public class MainActivity extends AppCompatActivity {
    boolean value = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setting content view and stuff
    }

    //the following should be overridden only if value == true. 
    //I can change the value to false by clicking a button in my view.
    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(ZeTarget.attachBaseContext(base,this));
    }

    public void stopOverriding (View view) {
        value = false;
    }

In my view, I have a button in my main activity layout which calls the stopOverriding() method on getting clicked:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:onclick="stopOverriding"
android:text="@string/change"/>

I have the same overriding attachBaseContext() method in all my activities. My question is, is it possible that after clicking the button in the Main Activity, I could stop overriding this attachBaseContext() method in all my activities?

Akeshwar Jha
  • 4,516
  • 8
  • 52
  • 91

1 Answers1

8

You can't make a runtime decision about whether a method is overridden (without generating classes dynamically, which is complicated [and I have no idea if it's supported in Dalvik]).

Just check your condition in the method:

protected void attachBaseContext(Context base) {
    if (this.value) {
        // Your special behavior
        super.attachBaseContext(ZeTarget.attachBaseContext(base,this));
    } else {
        // The super's behavior, as though you hadn't overridden the method
        super.attachBaseContext(base);
    }
}
T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Thanks, I get it. So, based on the behavior I want (override/not-override), I will have to declare the `value` true/false in the MainActivity class itself. How can I use this value I set in all other activities? Will I have to do the declaration separately in all other activities? – Akeshwar Jha Jan 07 '16 at 10:14
  • @Akeshwar: The condition can be anything that that method can access. So yes, `value` would work, or anything else it can check. – T.J. Crowder Jan 07 '16 at 10:17
  • I was just wondering about the best way to incorporate the `value` in all my activities. And I decided to store it in a file in my main activity and then accessing the file in all other ones. Thanks for the answer, it resolved my issue. – Akeshwar Jha Jan 07 '16 at 11:18
  • You should consider using SharedPreferences instead of a file, as it is the preferred way to store key-value sets for an application: http://developer.android.com/training/basics/data-storage/shared-preferences.html – Björn Kechel Jan 07 '16 at 12:18
  • Thanks for the response, @bjornson. I'm using SharedPreferences now. But I now have a new problem using them. Check this out: http://stackoverflow.com/questions/34664901/can-we-have-a-global-variable-with-respect-to-an-entire-android-application – Akeshwar Jha Jan 07 '16 at 20:52