Creating abstract Activity classes

16,478

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
    }
}
Share:
16,478
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
Author by

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, 2022

Comments

  • zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
    zzzzzzzzzzzzzzzzzzzzzzzzzzzzzz almost 2 years

    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?

  • Squonk
    Squonk about 13 years
    @user721378: I'd be interested to know how that works out with an Android Activity.
  • J.S. Taylor
    J.S. Taylor about 13 years
    Sorry 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
    Laurence Gonsalves about 13 years
    While 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 of Activity. Perhaps it's a bad idea (particularly with methods like onBackPressed that have non-empty default implementations that you might want to call via super) but it can certainly be done.