简体   繁体   中英

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. So lets say activity ActA is parent offering the framework and activity ActB is the child.

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. 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

 public class ActB extends ActA{

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

Hope this will help you;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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