从主屏幕返回APP时未调用onResume()和onRestart()

时间:2013-03-11 13:59:16

标签: android

我是Android新手。目前正在构建一款适用于Android的简单游戏。

我的问题是当按下主页按钮时,它会调用onPause(),有时会调用onPause()和onStop()。但是,当我尝试返回游戏时(通过单击主屏幕中的图标),既不调用onResume()或onRestart()。游戏保持渲染,但我无法与游戏互动。触摸事件无效。当我试图触摸按钮时,没有任何反应,几秒后,它说“没有响应”,然后我必须强行关闭。

顺便说一句。我还注意到,当我们去主屏幕时,会调用MainGamePanel中的surfaceDestroyed()函数。我认为这个函数会阻塞线程,但实际上线程在主屏幕上仍在运行。

有人可以告诉我为什么onRestart()或onResume()没有触发,以及为什么触摸事件在从主屏幕返回游戏后无效?

非常感谢你。

我重写onStop(),onPause(),onResume和onRestart(),以便在调用函数时在日志中输出消息:

@Override
    protected void onStop() {
        Log.d(TAG, "Stopping...");
        super.onStop();
    }

以下是我的活动和表面视图的代码。 我有一个ActivityMainGame扩展Activity:

public class ActivityMainGame extends Activity 
{
    private MainGamePanel mainGamePanel;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_game);

        Log.w(TAG, "Creating the game");

        //set our MainGamePanel as the view
        mainGamePanel = new MainGamePanel(this);
        setContentView(mainGamePanel);
    }
}

我创建了一个MainGamePanel扩展SurfaceView实现了SurfaceHolder.Callback,这个类启动一个线程来在画布上渲染东西:

public class MainGamePanel extends SurfaceView implements SurfaceHolder.Callback 
{
    private MainGameThread thread;

    public MainGamePanel(Context context) 
    {
        super(context);
        // adding the callback (this) to the surface holder to intercept events
        getHolder().addCallback(this);

        // create the game loop thread
        thread = new MainGameThread(getHolder(), this);

        // make the GamePanel focusable so it can handle events
        setFocusable(true);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) 
    {
        if (thread.getState() == Thread.State.NEW)
        {
            thread.setRunning(true);
            thread.start();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder)
    {
        boolean retry = true;
        while (retry)
        {
            try
            {
                thread.join();
                retry = false;
            }
            catch (InterruptedException e)
            {
                Log.d(TAG, e.getMessage());
            }
        }
    }

作为Adil的请求,我将错误消息放在logcat中。当我的游戏被强制关闭时,这些是消息:

03-11 15:04:21.491: E/ActivityManager(58): ANR in com.myslotgame (com.myslotgame /.ActivityMainGame)
03-11 15:04:21.491: E/ActivityManager(58): Reason: keyDispatchingTimedOut
03-11 15:04:21.491: E/ActivityManager(58): Load: 0.52 / 0.19 / 0.18
03-11 15:04:21.491: E/ActivityManager(58): CPU usage from 551359ms to 51ms ago:
03-11 15:04:21.491: E/ActivityManager(58):   system_server: 4% = 3% user + 0% kernel / faults: 5766 minor
03-11 15:04:21.491: E/ActivityManager(58):   adbd: 0% = 0% user + 0% kernel / faults: 55 minor
03-11 15:04:21.491: E/ActivityManager(58):   m.android.phone: 0% = 0% user + 0% kernel / faults: 17 minor
03-11 15:04:21.491: E/ActivityManager(58):   ndroid.launcher: 0% = 0% user + 0% kernel / faults: 1625 minor
03-11 15:04:21.491: E/ActivityManager(58):   qemud: 0% = 0% user + 0% kernel
03-11 15:04:21.491: E/ActivityManager(58):   events/0: 0% = 0% user + 0% kernel
03-11 15:04:21.491: E/ActivityManager(58):   zygote: 0% = 0% user + 0% kernel / faults: 1201 minor
03-11 15:04:21.491: E/ActivityManager(58):   id.defcontainer: 0% = 0% user + 0% kernel / faults: 77 minor
03-11 15:04:21.491: E/ActivityManager(58):   logcat: 0% = 0% user + 0% kernel
03-11 15:04:21.491: E/ActivityManager(58):   rild: 0% = 0% user + 0% kernel
03-11 15:04:21.491: E/ActivityManager(58):   ndroid.settings: 0% = 0% user + 0% kernel / faults: 9 minor
03-11 15:04:21.491: E/ActivityManager(58):   roid.alarmclock: 0% = 0% user + 0% kernel / faults: 14 minor
03-11 15:04:21.491: E/ActivityManager(58):   re-initialized>: 0% = 0% user + 0% kernel / faults: 7 minor
03-11 15:04:21.491: E/ActivityManager(58):   d.process.media: 0% = 0% user + 0% kernel / faults: 9 minor
03-11 15:04:21.491: E/ActivityManager(58):   com.android.mms: 0% = 0% user + 0% kernel / faults: 16 minor
03-11 15:04:21.491: E/ActivityManager(58):  +myslotgame: 0% = 0% user + 0% kernel
03-11 15:04:21.491: E/ActivityManager(58):  -myslotgame: 0% = 0% user + 0% kernel
03-11 15:04:21.491: E/ActivityManager(58): TOTAL: 7% = 6% user + 1% kernel + 0% irq + 0% softirq

2 个答案:

答案 0 :(得分:0)

onStop()无法保证被调用,因为Android有时必须在不事先通知的情况下在后台过早结束游戏,但始终会调用onPause()!因此,在onPause()方法中,执行停止时要执行的操作,例如结束渲染线程,但也可以放置if (isFinishing())之类的if语句,并执行操作,如果活动实际上正在结束。

注意:如果您的活动扩展了isFinishing()类,您的Activity类,则只能使用ActivityMainGame方法。< / p>

答案 1 :(得分:0)

我认为当你进入主屏幕时,会调用onPause和onStop以及onDestroy。你强行关闭后可以发布logcat吗?