This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together.
show
Example download
Download.csdn.net/download/lj…
Train of thought
1. First we need to use broadcast, send a broadcast, to say that we need to force offline 2. You need to register a broadcast, and when that broadcast is received, a dialog box pops up, then finish() all activities, and jump to LoginActivity. But now we don’t know which activity will receive this broadcast, so we need to define a BaseActivity. The broadcast receiver is then registered and destroyed in BaseActivity
Code implementation
1. Tool class ActivityCollector
import android.app.Activity
object ActivityCollector {
private val activities = ArrayList<Activity>()
/ / add
fun addActivity(activity: Activity) {
activities.add(activity)
}
/ / remove
fun removeActivity(activity: Activity) {
activities.remove(activity)
}
/ / finish all
fun finishAll(a) {
for (activity in activities) {
if(! activity.isFinishing) { activity.finish() } } activities.clear() } }Copy the code
2. BaseActivity
import android.content.*
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import com.example.kotlindemo4.LoginActivity
open class BaseActivity : AppCompatActivity() {
private lateinit var receiver: OfflineReceiver
override fun onCreate(savedInstanceState: Bundle?). {
super.onCreate(savedInstanceState)
ActivityCollector.addActivity(this)}/** * Registered in the onResume method, meaning that only activities at the top of the stack should be registered */
override fun onResume(a) {
super.onResume()
val intentFilter = IntentFilter("com.example.bddemo.OFFLINE")
receiver = OfflineReceiver()
registerReceiver(receiver, intentFilter)
}
/** * When the activity exits the top of the stack, the broadcast is not needed, so cancel */ in onPause
override fun onPause(a) {
super.onPause()
unregisterReceiver(receiver)
}
override fun onDestroy(a) {
super.onDestroy()
ActivityCollector.removeActivity(this)}inner class OfflineReceiver : BroadcastReceiver() {
override fun onReceive(context: Context? , intent:Intent?). {
if(context ! =null) {
AlertDialog.Builder(context).apply {
setTitle("Tip")
setMessage("Your account is logged in elsewhere. Please log in again.")
setCancelable(false)
setPositiveButton("Sure") { _, _ ->
ActivityCollector.finishAll()
val i = Intent(context, LoginActivity::class.java)
context.startActivity(i)
}
create()
show()
}
}
}
}
}
Copy the code
3. MainActivity
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.bddemo.BaseActivity
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?). {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn_out.setOnClickListener {
val intent = Intent("com.example.bddemo.OFFLINE")
sendBroadcast(intent)
}
}
}
Copy the code