[英]Android: call a function only on child activity from parent activity
My ParentActivity.java is like this 我的ParentActivity.java就是这样
public class ParentActivity extends Activity{
public void childOnlyMethod(){
Log.d(TAG,"child only method triggered in parent activity");
}
public void startChildActivtityButton(){
startActivity(new Intent(this, ChildActivity.class));
}
public void childOnlyMethodButton(){
childOnlyMethod();
}
}
And my ChildActivity.java is like this 我的ChildActivity.java就是这样
public class ChildActivity extends ParentActivity{
@Override
public void childOnlyMethod(){
Log.d(TAG,"child only method triggered in child activity");
}
}
The problem is when I press childOnlyMethodButton, childOnlyMethod() in both parent and child activity gets invoked I want it to be invoked on child only how can I achieve that? 问题是当我在父活动和子活动中同时按下childOnlyMethodButton,childOnlyMethod()时 ,我希望仅在子活动上才能对其进行调用吗?
@Override annotation does nothing, it is only used to tell the compiler and IDE that this method overrides its super class. @Override注释不执行任何操作,仅用于告诉编译器和IDE此方法将覆盖其超类。 Non-static methods are associated to objects, not classes.
非静态方法与对象而非类相关联。 Overriding means completely replace the method in its super class.
覆盖意味着完全替换其超类中的方法。 So if you invoke childOnlyMethod on ChildActivity, only child version will be invoked.
因此,如果您在ChildActivity上调用childOnlyMethod,则只会调用子版本。
I'm guessing you were actually clicking on the parent activity instance. 我猜您实际上是在单击父活动实例。 I don't really get the point why you want to invoke a child method on parent reference.
我真的不明白为什么要在父引用上调用子方法。 If you can post the real code, I can give you more precise answer.
如果您可以发布真实的代码,我可以给您更准确的答案。
However, you can try the following code. 但是,您可以尝试以下代码。 This example will only invoke child version childOnlyMethod on ChildActivity instance.
本示例将仅在ChildActivity实例上调用子版本childOnlyMethod。 But it will still invoke parent's childOnlyMethod if you click the button on ParentActivity.
但是,如果您单击ParentActivity上的按钮,它将仍然调用父级的childOnlyMethod。
public class ParentActivity extends Activity implements View.OnClickListener {
private static final String TAG = "ParentActivity";
private Button mStartChileButton;
private Button mButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStartChileButton = (Button) findViewById(R.id.startChileButton);
mButton = (Button) findViewById(R.id.button);
mStartChileButton.setOnClickListener(this);
mButton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.R.id.startChileButton:
startActivity(new Intent(this, ChildActivity.class));
break;
case R.id.R.id.button:
default:
childOnlyMethod();
break;
}
}
public void childOnlyMethod() {
Log.d(TAG, "Called from ParentActivity");
}
}
public class ChildActivity extends ParentActivity {
private static final String TAG = "ChildActivity";
@Override
public void childOnlyMethod() {
Log.d(TAG, "Called from ChildActivity");
}
}
If you are extending a parent class, all the methods in the parent class are passed down somewhat to the child class, if you don't want the method in the parent class to be implemented, first start by removing that @Override annotation in the child class above the method. 如果要扩展父类,则将父类中的所有方法稍微传递给子类,如果您不希望实现父类中的方法,请首先从删除@Override批注开始方法上面的子类。 Then use this snipped below, all it does is make the Method in the parent class either private or protected so that it can't be accessed in the child class.
然后在下面使用此代码片段,它所做的就是将父类中的Method设置为私有或受保护,以便无法在子类中对其进行访问。
public class ParentActivity extends Activity {
private void childOnlyMethod(){
Log.d(TAG,"child only method triggered in parent activity");
}
//OR
protected void childOnlyMethod(){
Log.d(TAG,"child only method triggered in parent activity");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.