为什么要通过Application类管理生命周期? [英] Why manage lifecycle through Application class?

查看:79
本文介绍了为什么要通过Application类管理生命周期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在查看一个聊天演示应用程序中的代码,我注意到他们创建了一个扩展应用程序的类,并且在onCreate()中是以下行

I was reviewing code from a chat demo application and I noticed that they created a class that extended Application, and inside the onCreate() was the following line

registerActivityLifecycleCallbacks(new ActivityLifecycleHandler());

然后我查看了ActivityLifecycleHandler类,并且有类似

I then looked at the ActivityLifecycleHandler class and there were methods like

public void onActivityDestroyed(Activity activity) {
}
public void onActivityPaused(Activity activity) {
}
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
}
public void onActivityStarted(Activity activity) {
}

然后在所有Activity类文件的onPause()中-也在onStart(),onResume()等中-例如,将引用Application类的生命周期。

Then in all the Activity class files, in onPause()--also in onStart(), onResume(), ect--for example the lifecycle from the Application class would be referenced.

@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Application.activityPaused();
}

我想知道这是什么以及它做什么?这是否意味着onPause()用于活动,而Application.acitvityPaused()用于整个应用程序?他们这样做有什么好处吗?我不明白这是什么意思。预先感谢。

I would like to know what this is and what it does? Does this mean that onPause() is for the Activity and Application.acitvityPaused() is for the entire application? Are their any advantages to doing this? I do not understand what the point of this is. Thanks in advance.

推荐答案

我不知道,但是已经Google搜索过。检查源。

I don't know, but have just Googled. Check the source.

https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/core/java/android/app/Application.java。

您可以看到所有这些只是调用您的回调方法,没有进一步的处理。这些注释还表明它的设计用途是用于处理状态持久性。

You can see that all this does is call your callback methods, there is no further processing. The comments also indicate that it's designed usage is for handling state persistence.

因此,您可以通过创建一个Activity基类来实现相同目的,该基类提供了活动回调的覆盖,然后扩展您的所有活动。

Therefore, you could achieve the same by creating an Activity base class which provides overrides of the activity callbacks then extend all of your activities from this.

public class BaseActivity extends Activity{
    @override
    public void onCreate(){
          super.onCreate(); // calls the framework method
    }
}

public class Activity1 extends BaseActivity{
    @override
    public void onCreate(){
         super.onCreate(); // calls the base method which in turn calls the framework method
         ...
    }
}

public class Activity2 extends BaseActivity{
    @override
    public void onCreate(){
         super.onCreate();
         ...
    }
}

这些方法将是要求进行所有活动,无论它们是否扩展基类。因此,该类生命周期将自动进行这些回调,而您无需执行任何其他操作。请注意,它仅扩展 Activity ,而不扩展 BaseActivity

These methods will be called for all activities, regardless of whether they extend a base class. So this class lifecycle will automatically make those callbacks without you having to do anything more. Note that it only extends Activity, not BaseActivity.

public class Activity3 extends Activity{
     @override
     public void onCreate(){
          super.onCreate();
          ...
     }
 }

因为您无法拥有Java中的多重继承,如果一个活动需要扩展所有活动的基类以外的内容,则必须开始链接可能很快变得混乱的回调链。

Since you cannot have multiple inheritance in Java,if an activity needed to extend something other than a base class for all activities, you would have to start chaining callbacks which could get messy quickly.

最后,因为 ActivityLifecycleCallbacks 是一个接口,并且由于您可以注册该接口的多个实现,所以可以使用一组回调来管理应用程序状态,或者使用一组回调来管理用户状态,也许
来管理服务或资源处理等,而不必在单个回调方法中混合该代码。

Finally, since ActivityLifecycleCallbacks is an interface, and since you can register multiple implemenations of that interface, you could have one set of callbacks to manage application state, another to manage user state, perhaps one to manage service or resource handling and so on without having to mix that code in a single call back method.

这篇关于为什么要通过Application类管理生命周期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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