扩展性及的ActivityGroup [英] Extend Activity and ActivityGroup

查看:119
本文介绍了扩展性及的ActivityGroup的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在写一个应用程序中,我有一组code,我想在我所有的活动和ActivityGroups可用。然而,要做到这一点,我已经延长我的活动:

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

现在的问题是,无论自定义$ C $词写在BaseActivity,我必须写在BaseActivityGroup相同太(在目前的实现)。这样却容易出现code-同步的问题,我相信没有一个很好的技术。

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.

那么,如何才能让我的扩展,以这样的方式,我只在 BaseActivity 编写自定义code和我的 BaseActivityGroup 扩展的ActivityGroup - 这是从 BaseActivity 类构思

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?

如果我观察一下Android的做到这一点,所以在的ActivityGroup 在Android中延伸的活动类。我还要写我的自定义的的ActivityGroup 类(称为的 BaseActivityGroup 的),实际上扩展了的 BaseActivity (这是一个扩展的活动)。

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).

任何意见/建议?

推荐答案

首先ActivityGroups都是坏的,不应该使用。它们德precated并且它是pferred使用单个活动与多个片段$ P $

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.

如果您必须通过实现委托模式使用的ActivityGroup,你可能是最好的。

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

创建能够处理所有的常用方法,如的onCreate,onResume委托和使用,在该基地。在这个例子中,我保存的引用,在委托的活动。这种循环引用可能不是pretties。另一种方法是通过对活动中委托的方法。

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);
    }
    ...
}

这篇关于扩展性及的ActivityGroup的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆