android 如何创建自己的Activity并扩展它?

发布于 2024-12-26 02:42:59 字数 503 浏览 2 评论 0原文

我需要创建一个扩展 Activity 的基类,它在我的应用程序中执行一些常见任务,并从中扩展我的活动,格式如下:

public BaseActivity 扩展了 Activity{...}

BaseActivity{...}

public SubActivity 扩展了SubActivity 中的 我需要为中定义的一些变量和 UI 组件赋值BaseActivity,我可能需要根据某些标志值为 SubActivity 定义不同的布局,另外(在 SubActivity 中)我想执行 asyncTask在BaseActivity中定义。

这可能吗?如果是,有任何教程可以帮助吗? 先感谢您

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

扬花落满肩 2025-01-02 02:42:59

您究竟想要实现什么目标?除了布局的某些变量或部分之外,有两个不同的活动具有共同的用户界面?

在这种情况下,我建议有一个基本的抽象活动和两个具体的继承子类。您在基本活动中定义所有常见行为,并为差异提供抽象方法,然后在实际实现中覆盖这些方法。

例如,对于具有不同布局资源的两个活动:

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

对于您想要特定于子类的每一位,您可以拥有更多抽象方法。

在我看来,这样做比拥有具体超类的具体子类要好得多:这可能会导致许多问题,并且通常难以调试。

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.

哭了丶谁疼 2025-01-02 02:42:59

这个问题已经有了很好的答案。
然而。我的答案是针对那些正在寻找可行示例的人。
这是完整的工作 -> 代码

在此处输入图像描述
我们在这里没有做任何新的事情,它就像任何其他继承场景一样(您希望在多个地方有一些共同的行为,但您只想将该行为编写一次)。

优点:
它确实提供了更好的代码可读性、可维护性等等。但并不是追求这些能力,如果你的大脑像瞪羚一样运转,它们对你来说并不重要。
我们追求的是真正的传承力量“控制”。 (这也是现实生活中发生的事情。父母控制孩子:))。

在我的示例中,我有两个活动 MainActivity 和 OtherActivity。
这两个活动都有不同的布局,但我希望它们都以一些动画或一些欢迎消息开始。

我们的首要任务是找出共同行为。
这里->用动画启动 Activity。
我们已经找到了共同的“事物”,现在我们将在 BaseClass 中编写该行为(AnimationActivity)。
MainActivity和OtherActivity将继承AnimationActivity。

所以代码看起来像`

基础活动

AnimationActivity {

  startAnimation()
  {  
    ....  
  } 
}

儿童活动

MainActivity extends AnimationActivity{

}

OtherActivity extends AnimationActivity{

}

这种设计方法提供了很多控制灵活性(修改器的力量)。

1) 控制: 将动画方法保留在 onCreate() 中
当您决定活动应以动画开始时。
将您的方法保留在 onCreate(Bundle bundle) 方法中。现在只需更改修饰符,您就可以控制子 Activity。
如果将修饰符保留为
最终:子活动将从父动画开始。
摘要:子活动必须提供自己的动画。
no 修饰符:子活动可以通过重写动画方法拥有自己的动画,否则子活动将拥有父动画。

2)灵活性:不要将动画方法保留在 onCreate() 中
您可以通过不在 onCreate(Bundle bundle) 中保留动画方法来提供子活动灵活性。
现在,活动可以灵活地拥有父动画或自己的动画,或者根本没有动画。
希望有帮助。
快乐学习。

`

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.

`

我偏爱纯白色 2025-01-02 02:42:59

是的,你可以,你应该记住基本的继承规则。如果您将内部 AsyncTask 活动和 BaseActivity 中定义的属性设置为受保护而不是私有,您将继承它们。从我现在看来,我认为你应该将 BaseActivity 设为一个抽象类,因为只有 subActivities 的实例才会真正被使用。

你应该开始并尝试一下,它会比你想象的更容易工作。如果您遇到任何问题,请询问。

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.

甜尕妞 2025-01-02 02:42:59

我找到了一种更简单的方法来解决@Guillaume 的问题。仅在 BaseActivity 中设置一次 ContentView,并且不要在扩展它的活动中设置它:

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

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.
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文