Android的 - 如何正确处理的onPause / onResume方法呢?正确处理、方法、Android、onResume

2023-09-05 09:24:07 作者:别打扰我泡崽

我有一个应用程序,开始播放声音和开始/恢复游戏在onResume()方法,但是我注意到的是,如果我的应用程序是最后一次运行应用程序时,我把手机进入待机状态(屏幕关闭) ,我只是preSS菜单按钮,检查时间,然后手机开始玩游戏和背景音乐(应用程序实际上不是可见的,只有日期/时间的屏幕,但onResume绝被称为在我的应用程序)。那我在这里做?有没有办法辨别什么是重新激活该应用程序,然后添加,只有在游戏开始时条件语句时,应用程序实际上是可见的?

I have an app that starts playing sounds and begins/resumes gameplay in the onResume() method, but what I'm noticing is that if my app was the last run application when I put the phone into standby (screen off), and I just press the Menu button to check the time, then the phone starts playing the game and sounds in the background (the app isn't actually visible, only the screen with the date/time is, yet onResume must have been called in my app). What am I to do here? Is there a way to discern what is reactivating the app, and then add a conditional statement that only starts the game when the app is actually visible?

下面是我的onResume一个片段:

Here is a snippet from my onResume:

@Override
    protected void onResume()
    {
        mySaveGame = Utilities.loadSavegame(this);

        //check the savegame
        if(mySaveGame!=null)
        {
            //start game using savegame values
            this.startFromSavedGame(mySaveGame.getIsLevelComplete());   
        }
        else
        {
            //run the 1st-run components
            this.startFirstRun();
        }

        super.onResume();
    }

我唯一能想到的,从开始时的屏幕被打开(即使在应用程序是不可见的)做以prevent的游戏是把this.finish()中的onPause最后一行()...但是,这迫使你要回去呢,因为precess本身被打死,每次重新启动应用程序(这是很好的,因为我的onPause保存永久数据,但它不是一个完美的解决方案)。

The only thing I can think of doing to prevent the game from starting whenever the screen gets turned on (even when the app isn't visible) is to put this.finish() as the last line in onPause()... but that forces you to restart the app every time you want to go back to it because the precess itself was killed (which is fine because my onPause saves persistent data, but it's not an elegant solution).

请帮忙。

推荐答案

你有没有考虑切换到 ONSTART()的onStop(),而不是 onResume()的onPause()

Have you considered switching to onStart() and onStop(), rather than onResume() and onPause()?