Creating abstract Activity classes
I am working on creating abstract activity classes 开发者_开发知识库for my application that I will reuse for each activity.
Super Class: android.app.Activity
My Abstract Class extends android.app.Activity myActivity
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.
精彩评论