1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
| open class MainActivity : AppCompatActivity() { private var position = 0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main)
findViewById<View>(R.id.btn_default).setOnClickListener { position = 0
Toast.makeText(this@MainActivity, "退到后台后切换", Toast.LENGTH_SHORT).show() } findViewById<View>(R.id.btn_alias1).setOnClickListener { position = 1
Toast.makeText(this@MainActivity, "退到后台后切换", Toast.LENGTH_SHORT).show() }
ProcessLifecycleOwner.get().lifecycle.addObserver(object : LifecycleEventObserver { override fun onStateChanged(source: LifecycleOwner, event: Lifecycle.Event) { when (event) { Lifecycle.Event.ON_RESUME -> { }
Lifecycle.Event.ON_START -> { Log.i("LifecycleObserver", "应用回到前台") }
Lifecycle.Event.ON_STOP -> { Log.i("LifecycleObserver", "应用退到后台") if (position == 0) { setDefaultAlias() } else { setAlias1() } }
else -> {} } } }) }
fun setDefaultAlias() { val packageManager = packageManager val name2 = ComponentName(this, NewMainActivity::class.java) packageManager.setComponentEnabledSetting( name2, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP ) val name1 = ComponentName(this, MainActivity::class.java) packageManager.setComponentEnabledSetting( name1, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0 ) }
fun setAlias1() { val packageManager = packageManager val name1 = ComponentName(this, MainActivity::class.java) packageManager.setComponentEnabledSetting( name1, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP ) val name2 = ComponentName(this, NewMainActivity::class.java) packageManager.setComponentEnabledSetting( name2, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, 0 ) } }
|