Android App在后台运行一段时间时切换到上一个屏幕

发布于 2025-02-12 15:07:13 字数 205 浏览 2 评论 0原文

我正在开发一个Android应用程序,该应用在主屏幕中显示一个地图视图,并具有两个按钮,一个用于切换到传感器屏幕(显示移动传感器的各种数据和图形),一个用于MAP实用程序。

在传感器数据屏幕中的后台运行应用程序(该应用程序定期将传感器数据存储到数据库文件中),一段时间后,当我将应用程序带到前景时,我发现它已切换回主屏幕。 ..为什么会发生这种情况?

我该如何避免它?

I am developing an android app, which in the main screen shows a map View and has two buttons, one for switching to the Sensors Screen( which displays various data and graphs from the mobile sensor) and one one for the map utility.

While running the app in the background in the sensor data screen (the app stores the sensor data into a database file periodically) after some period, when i bring the app in the foreground i find out that it has switched back to the main screen...why does this happen?

How can i avoid it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断肠人 2025-02-19 15:07:13
private fun setIntroScreenButtonListeners()
{
    introScreenLayoutBinding.SensorsBTN.setOnClickListener {
        switchToMainScreen()
    }

    introScreenLayoutBinding.MapBTN.setOnClickListener {
        onPause()
        switchToMapFragment()
   

}
}

private fun periodicStorageOfData()
{
    var handler = Handler()
    var runnableCode : Runnable = object : Runnable
    {
        override fun run() {
            GlobalScope.launch(Dispatchers.IO) {
                storeAllDataToSQlDatabase()
            }
            handler.postDelayed(this, 1000)
        }

    }
    handler.post(runnableCode)
}

    //fragment transaction method
    private fun switchToMapFragment()
    {
        sensorAppRunning = false
        mapFragment = MapFragment.newInstance()
//        locationMapFragment = LocationMapFragment()
        supportFragmentManager.beginTransaction().replace(introScreenLayoutBinding.fragmentFrameLayout.id , mapFragment).commit()
    }
private fun setIntroScreenButtonListeners()
{
    introScreenLayoutBinding.SensorsBTN.setOnClickListener {
        switchToMainScreen()
    }

    introScreenLayoutBinding.MapBTN.setOnClickListener {
        onPause()
        switchToMapFragment()
   

}
}

private fun periodicStorageOfData()
{
    var handler = Handler()
    var runnableCode : Runnable = object : Runnable
    {
        override fun run() {
            GlobalScope.launch(Dispatchers.IO) {
                storeAllDataToSQlDatabase()
            }
            handler.postDelayed(this, 1000)
        }

    }
    handler.post(runnableCode)
}

    //fragment transaction method
    private fun switchToMapFragment()
    {
        sensorAppRunning = false
        mapFragment = MapFragment.newInstance()
//        locationMapFragment = LocationMapFragment()
        supportFragmentManager.beginTransaction().replace(introScreenLayoutBinding.fragmentFrameLayout.id , mapFragment).commit()
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文