在我的MainActivity类中,attachBaseContext如果在我的视图中按下按钮,我想停止覆盖该方法.
情况如下:
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;
}
Run Code Online (Sandbox Code Playgroud)
在我看来,我的主活动布局中有一个按钮,可以stopOverriding()在单击时调用该方法:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:onclick="stopOverriding"
android:text="@string/change"/>
Run Code Online (Sandbox Code Playgroud)
在我的attachBaseContext()所有活动中,我都有相同的重写方法.我的问题是,是否有可能在单击主活动中的按钮后,我可以attachBaseContext()在所有活动中停止覆盖此方法?
你不能做出关于方法是否被重写的运行时决定(没有动态生成类,这很复杂[我不知道Dalvik是否支持它]).
只需检查方法中的条件:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1835 次 |
| 最近记录: |