android how to create my own Activity and extend it?

64.3k views Asked by At

I need to create a base class that extends Activity which does some common tasks in my application and extend my activities from it,in the following form:

public BaseActivity extends Activity{....}

public SubActivity extends BaseActivity{...}

in SubActivity I need to give values to some variables and UI components defined in BaseActivity, I may need to define a different layout for SubActivity according to some flag value, also(in SubActivity ) I want to execute asyncTask that is defined in BaseActivity.

is this possible? if yes, is there any tutorial that may help? thank you in advance

4

There are 4 answers

12
Guillaume On BEST ANSWER

What exactly are you trying to achieve? Having two different activities with a common ui, except for some variables or parts of the layout?

In this case, I suggest having a base abstract activity, and two concrete inherited subclasses. You define all the common behaviour in the base activity, and have abstract methods for the differences, which you then override in your actual implementations.

For example, for two activities with different layout resources:

public abstract class BaseActivity extends Activity {
    @Override
    public void onCreate(bundle) {
        super.onCreate(bundle);
        setContentView(getLayoutResourceId());
    }

    protected abstract int getLayoutResourceId();
}

public class Activity1 extends BaseActivity {
    @Override
    public void onCreate(bundle) {
        super.onCreate(bundle);
        // do extra stuff on your resources, using findViewById on your layout_for_activity1
    }

    @Override
    protected int getLayoutResourceId() {
        return R.layout.layout_for_activity1;
    }
}

You can have a lot more abstract methods, for every bit you want specific to your subclasses.

Doing that is, in my opinion, a lot better than having a concrete subclass to a concrete superclass: that can lead to many problems and is usually difficult to debug.

0
J. Maes On

Yes you can, you should just keep in mind the basic inheritance rules. You will inherit the inner AsyncTask activity and the properties defined in the BaseActivity if you make them protected instead of private. From what I see now I think you should make BaseActivity an abstract class, as only instances of subActivities will be really used.

You should just start and try it, it'll come and work easier than you think. If you stumble upon any problems, just ask.

1
Seagull On

I have found an easier way to @Guillaume's solution. Set ContentView only once in your BaseActivity and do not set it in the activities that extend it:

public abstract class BaseActivity extends Activity {
    @Override
    public void onCreate(bundle) {
        super.onCreate(bundle);
        setContentView(activity_main);
    }
}

public class Activity1 extends BaseActivity {
    @Override
    public void onCreate(bundle) {
        super.onCreate(bundle);
        // setContentView(activity_activity1)  // Do NOT call this.
    }
}
0
Rohit Singh On

This question already has very good answers.
However. my answer is for those people who are looking for some working example.
Here is the full working -> CODE

enter image description here
We are not doing anything new here, it is just like any other inheritance scenario (You want some common behavior at multiple places but you want to write that behavior only once).

ADVANTAGE: It does provide better code readability, maintainability and blah blah. But are not after these -ibility, They won't matter to you if your brain runs like a gazelle.
We are after the real power of inheritance “CONTROL”. (That’s what happens in real life too. Parent controlling child :) ) .

In my example, I have two Activities MainActivity and OtherActivity. Both Activities has a different layout but I want both of them to start with some animation or some welcome message.

Our first task is to find out the common behavior. here -> Start Activity with animation.
We have found the common “thing”, now we will write that behavior in BaseClass (AnimationActivity).
MainActivity and OtherActivity will inherit AnimationActivity.

So the code would look like `

BaseActivity

AnimationActivity {

  startAnimation()
  {  
    ....  
  } 
}

Child Activities

MainActivity extends AnimationActivity{

}

OtherActivity extends AnimationActivity{

}

This design approach provides a lot of Control and Flexibility (POWER OF MODIFIER).

1) CONTROL: Keep animation method inside onCreate() When you decide that Activities should be started with Animation. Keep your method inside onCreate(Bundle bundle) method. Now just by changing the modifier, you can control the child Activities.
If you keep modifier as
final: Child activities will start with parent Animation.
abstract: Child activities will have to give their own animation.
no modifier: Child activities can have their own animation by overriding animation method, Otherwise the child will have parent animation.

2)Flexibility: Don't keep animation method inside onCreate() You can provide child activities flexibility by not keeping animation method inside onCreate(Bundle bundle). Now activities can have the flexibility to have parent Animation or their own animation or no animation at all.
Hope it helps.
Happy learning.

`