简体   繁体   中英

Android: onResume in MainActivity not called

My app has a mainactivity ...

    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();
    }

}

and the gameLoopThread is

 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();
        }
    }

}

anyway the onResume() in the mainActivity isn't getting called: I determined this by putting a breakpoint there. so the game pauses when i exit the app, but when i return to the app, it's just a paused screen of the game and it doesn't resume again: I want it to resume when i re-enter the app (using the recent application screen). Here is the logcat in case there's anything of signifance.

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...

I am not sure whether this is a proper solution. But i have once heard about the similar bug. you can try using the following tag in the activity manifest

android:noHistory="false"

Implement another procedure that's called in your override of OnResume(). The latter is not intended to be called by you, it's a convenience method that tidies up or readies the activity when its state changes to resume. A lot like onStart() through to onDestroy()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM