Android:未调用MainActivity中的onResume

时间:2013-12-23 04:01:14

标签: java android multithreading android-activity

我的应用有主动 ...

    public class MainActivity extends Activity {

    GameView gameView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        gameView = new GameView(this);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(gameView);

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        // we dont know yet tho
        return true;
    }

    @Override
    protected void onPause() {
        super.onPause();
        gameView.gameLoopThread.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        gameView.gameLoopThread.onResume();
    }

}

gameLoopThread

 public class GameLoopThread extends Thread {
    private GameView view;
    private boolean isRunning = false;
    static final long FPS = 32;
    private boolean mPaused = false;
    private Object mPauseLock;

    public GameLoopThread(GameView view) {
        this.view = view;
        mPauseLock = new Object(); 
    }

    public void setIsRunning(boolean isRunning) {
        this.isRunning = isRunning;
    }

    @SuppressLint("WrongCall")
    @Override
    public void run() {
        long ticksPS = 1000 / FPS;
        long startTime;
        long sleepTime;
        while (isRunning) {
            Canvas c = null;
            startTime = System.currentTimeMillis();
            try {
                c = view.getHolder().lockCanvas();
                synchronized (view.getHolder()) {
                    view.onDraw(c);
                }
            } finally {
                if (c != null) {
                    view.getHolder().unlockCanvasAndPost(c);
                }
            }
            sleepTime = ticksPS - (System.currentTimeMillis() - startTime);
            try {
                if (sleepTime > 0)
                    sleep(sleepTime);
                else
                    sleep(10);
            } catch (Exception e) {
            }

            synchronized (mPauseLock) {
                while (mPaused) {
                    try {
                        mPauseLock.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }

    public void onPause() {
        synchronized (mPauseLock) {
            mPaused = true;
        }

    }

    public void onResume() {
        synchronized (mPauseLock) {
            mPaused = false;
            mPauseLock.notifyAll();
        }
    }

}

无论如何,mainActivity中的onResume()没有被调用:我通过在那里放置断点来确定这一点。因此当我退出应用程序时游戏暂停,但是当我返回应用程序时,它只是暂停的游戏屏幕并且它不会再次恢复:我希望它在我重新进入应用程序时恢复(使用最近的应用程序屏幕)。 这是logcat,以防有任何重要意义。

12-22 21:39:09.382: I/System.out(10992): Sending WAIT chunk
12-22 21:39:09.402: I/dalvikvm(10992): Debugger is active
12-22 21:39:09.582: I/System.out(10992): Debugger has connected
12-22 21:39:09.592: I/System.out(10992): waiting for debugger to settle...
12-22 21:39:09.792: I/System.out(10992): waiting for debugger to settle...
12-22 21:39:09.985: I/System.out(10992): waiting for debugger to settle...
12-22 21:39:10.185: I/System.out(10992): waiting for debugger to settle...
12-22 21:39:10.392: I/System.out(10992): waiting for debugger to settle...
12-22 21:39:10.592: I/System.out(10992): waiting for debugger to settle...
12-22 21:39:10.795: I/System.out(10992): waiting for debugger to settle...
12-22 21:39:10.994: I/System.out(10992): debugger has settled (1313)
12-22 21:39:12.343: D/dalvikvm(10992): threadid=1: still suspended after undo (sc=1 dc=1)
12-22 21:42:21.922: W/ActivityThread(11454): Application com.dotdodge is waiting for the debugger on port 8100...

2 个答案:

答案 0 :(得分:0)

我不确定这是否是一个合适的解决方案。但我曾经听说过类似的错误。您可以尝试在活动清单中使用以下标记

android:noHistory="false"

答案 1 :(得分:0)

实现在覆盖OnResume()时调用的另一个过程。后者不是为了让你调用,它是一种方便的方法,可以在状态变为恢复时整理或准备活动。很像onStart()到onDestroy()