-1

I'm writing an application in which i have a set of code which i want to be available in all of my Activities and ActivityGroups. However, to achieve this, I have extended my activities as:

//custom Activity
public abstract class BaseActivity extends Activity
//custom ActivityGroup
public abstract class BaseActivityGroup extends ActivityGroup

//implemented activities in my app
public class PickUser extends BaseActivity
//and
public class Home extends BaseActivityGroup

Now the thing is, whatever the custom code i write in BaseActivity, I have to write the same in BaseActivityGroup too (as in current implementation). This is prone to code-sync problems and i believe not a good technique.

So, how can i make my extensions in such a way that I only write custom code in BaseActivity and my BaseActivityGroup extends ActivityGroup - which is conceived from BaseActivity class?

If i observe how android does this, so the ActivityGroup in android extends Activity class. And I also want to write my custom ActivityGroup class (known as BaseActivityGroup) that actually extends BaseActivity (which is an extended Activity).

Any ideas/suggestions?

waqaslam
  • 67,549
  • 16
  • 165
  • 178

4 Answers4

0

Add an extra final class, called Base. This one will only contain methods to be called by the other Base classes, such as for instance:

public static boolean createOptionsMenu(final Menu menu,
        final MenuInflater inflater) {
    inflater.inflate(R.menu.main_menu, menu);
    return true;
}

Then, in your BaseActivity and BaseActivityGroup classes, you would call:

@Override
public final boolean onCreateOptionsMenu(final Menu menu) {
    return Base.createOptionsMenu(menu, getMenuInflater());
}

Hope it helps!

jcxavier
  • 2,232
  • 1
  • 15
  • 24
  • actually, I dont need static methods. I have written custom methods to offer easy **service binding**, **authentic session renewal** and some **broadcast listening** in my base classes which are vital to be available with in the class file – waqaslam Feb 23 '12 at 12:14
0

Just Extend everything to BaseActivity including BaseGroupActivity as everything is a child of Activity in android

Altaf
  • 5,150
  • 10
  • 39
  • 55
  • if i extend everything to **BaseActivity** then all my classes will be the instances of **Activity** and this is not the case i want since i need **ActivityGroup** too – waqaslam Feb 23 '12 at 12:16
0

First of all ActivityGroups are bad and should not be used. They are deprecated and it is preferred to use a single activity with multiple fragments.

If you must use an activitygroup you are probably best of by implementing a delegate pattern.

Create a delegate that handles all the common methods such as onCreate, onResume and use that in the bases. In this example I save a reference to the activity in the delegate. This circular referencing might not be the pretties. An alternative is to pass on the activity to the methods in the delegate.

public class ActivityDelegate() {
    private Activity mActivity;

    public ActivityDelegate(final Activity activity) {
        mActivity = activity;
    }
    public void onCreate(final Bundle savedInstanceState) {
        // Do stuff.
    }
}


public abstract class BaseActivity extends Activity {
    private ActivityDelegate mDelegate = new ActivityDelegate(this);

    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDelegate.onCreate(savedInstanceState);
    }
    ...
}

public abstract class BaseActivityGroup extends ActivityGroup {
    private ActivityDelegate mDelegate = new ActivityDelegate(this);

    public void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDelegate.onCreate(savedInstanceState);
    }
    ...
}
Sebastian Olsson
  • 836
  • 6
  • 10
  • but i need access to activity methods too which i cant get hold in ActivityDelegate class. isn't it? – waqaslam Feb 23 '12 at 12:19
  • I edited the example so the activity is saved as a reference in the delegate. Then you can callback to all common Activity methods you need. – Sebastian Olsson Feb 23 '12 at 12:21
  • but then how to handle activity life-cycle? i mean `super.onCreate(savedInstanceState);` is missing in your implementation. How to resolve this? – waqaslam Feb 23 '12 at 13:30
  • My example wasn't a full implementation, but I updated it a bit. You still must call super.onCreate(savedInstanceState) and similar in both BaseActivity and BaseActivityGroup since they are protected and can therefore not be called from the delegate. This seems like a very small price to pay since you should do that anyhow. – Sebastian Olsson Feb 23 '12 at 13:38
  • hmmm, this approach seems like a hack but works fine. in order to synchronize the methods in base classes and their calls in delegate class, I've introduced an interface which is implemented by base and delegate class. anyhow, thanks for another way to deal with this – waqaslam Feb 24 '12 at 09:06
  • It's not a hack. The delegate pattern is a common pattern and used all over in the OO world (especially in Java). It is an alternative to inheritance and works well to reduce the levels of inheritance as well as hiding methods from the delegate in a client friendly api. – Sebastian Olsson Feb 24 '12 at 09:13
0

you can put your login in a separate file under a method. now call the same method from both BaseActivity and BaseActivityGroup if you need activity instance in file . pass context through constructor