让对象监听 Activity 生命周期事件?

发布于 2024-10-29 02:07:54 字数 678 浏览 2 评论 0原文

我编写的类之一需要在发生以下 Activity 事件时做出反应:

  • onStart()
  • onPause()
  • onResume()
  • onStop()

本身的事件做出反应:

public class Activity extends ApplicationContext
{
    protected void onCreate(Bundle savedInstanceState);

    protected void onStart();

    protected void onRestart();

    protected void onResume();

    protected void onPause();

    protected void onStop();

    protected void onDestroy();
}

我可以对Activity code>Activity 我可以告诉相关对象发生了某个事件,但我不喜欢这个想法:它要求开发人员在我的 object/class 之外实现逻辑。理想情况下,我希望该对象负责注册这些事件并将其自身设置为侦听器,独立于 Activity

有什么想法吗?提前致谢。

One of the classes I've written needs to react when the following Activity events occur:

  • onStart()
  • onPause()
  • onResume()
  • onStop()

I can react to those on the Activity itself:

public class Activity extends ApplicationContext
{
    protected void onCreate(Bundle savedInstanceState);

    protected void onStart();

    protected void onRestart();

    protected void onResume();

    protected void onPause();

    protected void onStop();

    protected void onDestroy();
}

From the Activity I could tell the object in question that a certain event has occurred, but I don't like this idea: it requires the developer to implement the logic outside my object/class. Ideally I would like the object to be responsible for registering these events and set itself as a listener, independent of the Activity.

Any ideas? Thanks in advance.

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

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

发布评论

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

评论(3

荒岛晴空 2024-11-05 02:07:54

API 级别 14 有 Application.ActivityLifecycleCallbacks。
在此之前,抱歉,没有。
如果您希望将您的类提供给其他人,您将需要提供扩展最常见活动的抽象类,或者让他们在自己的活动生命周期方法中进行某些调用,例如

protected void onPause() {
  super.onPause();
  yourClassInstance.onPause();
}

也可以使其更通用,并创建抽象类接受 NotifyingActivity.LifecycleListener 的 NotifyingActivity 类,并使您的类实现此类侦听器并在其构造函数中注册自身。

API level 14 has Application.ActivityLifecycleCallbacks.
Before that, afaik, sorry, no.
If you wish to offer your class to others, you will need to either provide abstract classes that extend the most common Activities, or have them put certain calls in their own Activity's lifecycle methods, like

protected void onPause() {
  super.onPause();
  yourClassInstance.onPause();
}

May as well make it more general, and create abstract NotifyingActivity classes that accept NotifyingActivity.LifecycleListener's, and make your class implement such a listener and register itself in its constructor.

月竹挽风 2024-11-05 02:07:54

利用自 API 级别 14 以来引入的 ActivityLifecycleCallbacks

这就是界面看起来像现在这样。

public interface ActivityLifecycleCallbacks {
    void onActivityCreated(Activity activity, Bundle savedInstanceState);
    void onActivityStarted(Activity activity);
    void onActivityResumed(Activity activity);
    void onActivityPaused(Activity activity);
    void onActivityStopped(Activity activity);
    void onActivitySaveInstanceState(Activity activity, Bundle outState);
    void onActivityDestroyed(Activity activity);
}

您需要做的只是实现接口方法并将其注册到您的应用程序上。

public class MyApplication extends Application implements ActivityLifecycleCallbacks {
    @Override
    public void onCreate() {
         super.onCreate();
         this.registerActivityLifecycleCallbacks(this);
    }

    //implement call back methods. 
}

Leverage ActivityLifecycleCallbacks that has been introduced since API level 14.

This is what the interface looks like now.

public interface ActivityLifecycleCallbacks {
    void onActivityCreated(Activity activity, Bundle savedInstanceState);
    void onActivityStarted(Activity activity);
    void onActivityResumed(Activity activity);
    void onActivityPaused(Activity activity);
    void onActivityStopped(Activity activity);
    void onActivitySaveInstanceState(Activity activity, Bundle outState);
    void onActivityDestroyed(Activity activity);
}

What you need to do is simply to implement the interface methods and register it on your application.

public class MyApplication extends Application implements ActivityLifecycleCallbacks {
    @Override
    public void onCreate() {
         super.onCreate();
         this.registerActivityLifecycleCallbacks(this);
    }

    //implement call back methods. 
}
森林散布 2024-11-05 02:07:54

您可以使用 这个库 来完成您想要做的事情,而无需编写代码在您的活动或基本活动中。

而且使用起来非常简单:

ActivityListener.bind(mActivity).with(mCallback);

也许它可以帮助某人

You can use this library that does exactly what you're trying to do, without having to write code in your activity or in your base activity.

And is very simple to use:

ActivityListener.bind(mActivity).with(mCallback);

Maybe it helps someone

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