为什么在启动操作调用时有一个额外的 onPause onResume?

Why is there an extra onPause onResume when launching action call?

提问人:Naveed 提问时间:11/15/2023 更新时间:11/15/2023 访问量:20

问:

我有一个简单的设置,可以启动拨号器以使用ACTION_CALL


   override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_launch_dialer)
        Log.d("TAG", "onCreate")
        findViewById<Button>(R.id.btn_launch).setOnClickListener {
            Log.d("TAG", "Launching Call")
            val uri = Uri.parse("tel:3332223333")
            val intent = Intent(Intent.ACTION_CALL, uri)
            startActivity(intent)
        }
    }

    override fun onResume() {
        super.onResume()
        Log.d("TAG", "onResume")
    }
    
    override fun onPause() {
        super.onPause()
        Log.d("TAG", "onPause")
    }

我正在记录每个生命周期并看到奇怪的行为。一旦用户单击启动按钮,似乎就会有额外的内容。onPauseonResume

单击上面的启动按钮时,我得到以下日志输出。

Launching Call
onPause
onResume <-- This is unexpected
onPause
onStop

我无法弄清楚为什么生命周期会转换回初始 .有什么方法可以防止这种情况发生吗?onResumeonPause

其他上下文: 我正在尝试做一些工作,但由于意外的调用,它被重复了onPauseonResume

android android-intent android-lifecycle

评论

0赞 David Wasser 11/17/2023
启动呼叫时是否发生方向更改?也许您的应用程序处于横向模式,而手机应用程序处于纵向模式?
0赞 Naveed 11/17/2023
不,没有方向变化,我已经仔细检查过了。此外,启动任何其他活动不会导致此行为。这似乎只是呼叫意图。

答:

0赞 Md Eusuf Uddin 12/11/2023 #1

当您回到 App 中时,您无法避免调用 onResume()。因为 Activity lifecycle 使用此行为。我想,这个链接会对你有所帮助

Android 活动生命周期 - 所有这些方法的用途是什么?