具有 activity 继承的广播接收器

Broadcast receiver with activity inheritance

我有一个问题。我在每个活动中都有 2 个活动,我初始化广播接收器以获取互联网连接更改。收到更改后,我通过在屏幕上显示一些文本来通知用户。

为了不重复写代码,我创建了继承自AppCompatActivity的BroadcastActivity

现在我使用 super() 将视图传递给 BroadcastActivity 并使用视图进行操作(更改文本、可见性等)。

问题是:这样做是最佳做法还是有更优雅的解决方案?是否会导致内存泄漏?

感谢您的帮助!

BroadcastActivity 示例

open class BroadcastActivity : AppCompatActivity(), OnConnectionChangeListener {
    //some code
}

和activity的例子(二级activity结构与此类似)

public class MainActivity extends BroadcastActivity{
    //some code
}

P.S。 BroadcastActivity 在 Kotlin 上编写,MainActivity 在 Java.

上编写

P.S。 2 目前我不能切换到单身 activity 并寻找临时解决方案。

为了进一步阐述我的评论,这里有一些“更好”的东西(同样,在我个人看来)...

ActivityKtx.kt:

fun Activity.applyBroadcastReceiver(broadcastReceiver: BroadcastReceiver) {
    lifecycle.addObserver(
        BroadcastReceiverObserver(
            BroadcastReceiverConfigurationImpl(broadcastReceiver, this)
        )
    )
}

BroadcastReceiverObserver.kt:

/**
 * Class implementing [DefaultLifecycleObserver] which is in charge of delegating setting the
 * [BroadcastReceiver]'s registration status.
 * Registers in [Activity.onResume] and unregisters in [Activity.onPause]
 */
class BroadcastReceiverObserver(
    val broadcastReceiverConfig: BroadcastReceiverConfiguration
) : DefaultLifecycleObserver {
    var isBroadcastReceiverRegistered = false

    override fun onResume(owner: LifecycleOwner) {
        super.onResume(owner)
        if (isBroadcastReceiverRegistered.not()) {
            broadcastReceiverConfig.registerBroadcastReceiver()
            isBroadcastReceiverRegistered = true
        }

    }

    override fun onStop(owner: LifecycleOwner) {
        if (isBroadcastReceiverRegistered) {
            isBroadcastReceiverRegistered = false
            broadcastReceiverConfig.unregisterBroadcastReceiver()
        }
        super.onStop(owner)
    }

    // add same things for onStart() and onPause()
}

/**
 * Base class for handling a [BroadcastReceiver] registration status
 */
abstract class BroadcastReceiverConfiguration {
    abstract fun registerBroadcastReceiver()
    abstract fun unregisterBroadcastReceiver()
}

/**
 * Base class for handling the broadcast receiver
 *
 * @see [BroadcastReceiverObserver]
 */
class BroadcastReceiverConfigurationImpl constructor(
    private val broadcastReceiver: BroadcastReceiver,
    private val hostActivity: Activity
) : BroadcastReceiverConfiguration() {
    
    override fun registerBroadcastReceiver() {
        //ConnectivityManager.CONNECTIVITY_ACTION - deprecated
        hostActivity.registerReceiver(broadcastReceiver, IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION))
    }

    override fun unregisterBroadcastReceiver() {
        hostActivity.unregisterReceiver(broadcastReceiver)
    }
}

然后像这样使用它:

SomeActivity.kt:

class SomeActivity extends AppCompatActivity {
    
    override fun onCreate() {
        super.onCreate()
        applyBroadcastReceiver(myBroadcastReceiver)
    }

瞧。零需要子类化 Activity / AppCompatActivity / MyBaseActivity 或其他任何东西。