简体   繁体   English

为什么在活动中继承会导致活动功能不起作用

[英]Why inheritance in Activities makes activity function not work

I have 2 activity classes, one being abstract that offers a kind of framework to the other classes that will have similar behavior. 我有2个活动类,一个是抽象类,它为其他具有相似行为的类提供了一种框架。 So lets say activity ActA is parent offering the framework and activity ActB is the child. 因此,可以说活动ActA是提供框架的父级,活动ActB是子级。

I have the framework activity like the following: 我的框架活动如下:

public class ActA extends Activity{
    @Override
    protected void onCreate(Bundle b){
        super.onCreate(b);
        // do stuff for setting up framework
    }
}

Then I have a child activity like so: 然后,我有一个儿童活动,如下所示:

public class ActB extends ActA{
    @Override
    public void onCreate(Bundle b){
        super.onCreate(b);
        // do other stuff
    }
}

The code commented like do other stuff is not actually called. do other stuff被注释的代码实际上并未被调用。 What is the problem as to me it looks legit? 对我来说,看起来合法的是什么问题?

You can try it another way like 您可以尝试其他方式

 public abstract class ActA extends Activity{
      @Override
      protected void onCreate(Bundle b){
          super.onCreate(b);
          // do stuff for setting up framework
         callForStuff();
      }


 protected abstract void callForStuff();

}

Then you need not call onCreate() method again.Only you have to do it like 这样就不必再次调用onCreate()方法了,只需要像

 public class ActB extends ActA{

    @Override
    protected void callForStuff(){
      // abstract method
    }
}

Hope this will help you; 希望这个能对您有所帮助;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM