Android 何时注销LocalBroadcastManager接收器?

Android 何时注销LocalBroadcastManager接收器?,android,android-activity,android-lifecycle,Android,Android Activity,Android Lifecycle,有人能给我举个例子,说明如何在活动类中正确注销本地广播管理器接收器 Android开发者培训建议您这样做: @Override public void onPause() { super.onPause(); // Always call the superclass method first // When I should to unregister LocalBroadcastManager Receiver before or after super.onP

有人能给我举个例子,说明如何在
活动
类中正确注销
本地广播管理器
接收器

Android开发者培训建议您这样做:

 @Override
    public void onPause() {
    super.onPause();  // Always call the superclass method first

    // When I should to unregister LocalBroadcastManager Receiver before or after super.onPause()?
}

我看到了谷歌的例子,但我不明白什么时候应该在
super.onPause()之前注销
LocalBroadcastManager
receiver
或之后以及
ondestory
方法之前
super.ondestory()还是之后

提前谢谢

更新:
我在
onResume()
方法中注册
LocalBroadcastManager
receiver

这取决于您何时注册。基本上,如果您注册了,即
onResume()
,那么您就可以取消注册
onPause()
。如果在
onCreate()
中注册,则在
onDestroy()
中注销。我建议您使用
onResume()/onPause()
,除非您知道需要其他方式

以及调用super的方法。在“创建”方法
onCreate
中,首先调用super的方法。在类似于onDestroy的“Destruction”方法中,你称super为最后一件事。

正如官方文件所说():

如果在Activity.onResume()实现中注册接收者,则应在Activity.onPause()中注销该接收者。(暂停时不会收到意图,这将减少不必要的系统开销)。不要在Activity.onSaveInstanceState()中注销,因为如果用户在历史堆栈中向后移动,则不会调用此函数


另外,如果您的接收者在清单中静态声明,那么您就不必担心注册/注销问题了

I在onResume()中注册它,但问题是我应该在
super.onDestroy()之前何时注销它
super.onPause()
还是在超类方法之后?在
super.onPause()
之前,我会先在
onPause()中注销,但为什么谷歌的培训说你应该总是先调用超类方法呢?你是先调用super.onPause()还是最后调用super.onPause()其实并不重要。在
onPause()
中,它不会在
onDestroy()中注销,所以最好坚持一种模式
 @Override
    public void onDestroy() {

        // If the DownloadStateReceiver still exists, unregister it and set it to null
        if (mDownloadStateReceiver != null) {
            LocalBroadcastManager.getInstance(this).unregisterReceiver(mDownloadStateReceiver);
            mDownloadStateReceiver = null;
        }

        ...

        // Must always call the super method at the end.
        super.onDestroy();
    }