Creating abstract Activity classes
Solution 1
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.
Solution 2
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
}
}
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
ADO.NET, MS SQL SERVER, Windows Forms Applications(VB and C#), Visual FoxPro, ASP.NET, WCF Web Services, Windows Mobile, and Android 2.2 & 3.0. When you sort all of Stackoverflow's members alphabetically, I am last. Check out my Android calculator. It can perform calculations in any numeral system from base 2 to base 36. It is free.Any Base Calculator Add me on google plus and I will add you into my developers circle.
Updated on June 25, 2022Comments
-
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz almost 2 years
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
likeonCreate()
andonStart()
?Is this possible in Java?
-
Squonk about 13 years@user721378: I'd be interested to know how that works out with an Android Activity.
-
J.S. Taylor about 13 yearsSorry it's an activity class. Please forget about that i said about constructors or setters. You can pass interface class names through intent, but it makes everything much more complex. Why don't you just create an abstract method with a different name?
-
Laurence Gonsalves about 13 yearsWhile the technique presented here is a good one, I'm a bit baffled by the "Not really" in this answer. You can make
onCreate
, etc. abstract in an abstract subclass ofActivity
. Perhaps it's a bad idea (particularly with methods likeonBackPressed
that have non-empty default implementations that you might want to call viasuper
) but it can certainly be done.