开发者

Creating abstract Activity classes

I am working on creating abstract activity classes 开发者_开发知识库for my application that I will reuse for each activity.

  1. Super Class: android.app.Activity

  2. My Abstract Class extends android.app.Activity myActivity

  3. Example activty in my application extends myActivity.

I will have 10-20 of these exampleActivity.

How can I write my abstract class (#2) to force my example class to override methods in android.app.Activity like onCreate() and onStart()?

Is this possible in Java?


Not really.

However you can create abstract functions myOnCreate and myOnStart and call those in your abstract class implementation of onCreate and onStart.

You might also want to make onCreate/onStart final, although it's difficult to see what the benefit is of forcing myOnCreate instead of onCreate.


This is possible,

public abstract class MyActivity extends android.app.Activity
{

    public abstract void onCreat(...);
    public abstract void onStart(...);
}

public class OtherActivity extends MyActivity
{
    public void onCreate(...)
    {
        //write your code
    }

    public void onStart(...)
    {
      //write your code
    }
}


Nop. It's more elegant to create an interface with onCreate and onStart methods instead if you need force others to override in their implementation.

And pass the interface instance to your abstract activity class throught constructors or setters.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜