Android onResume not called
Asked Answered
W

2

0

I'm developing an app for Android, and I want it to pause the music when you press the home button (onPause). That works fine, but then when I try to start up the game again, onResume, onRestart, onStart, onRestoreInstanceState and onCreate are never called and it tells me that the application is not responding. There are no Exceptions shown in the LogCat... So I have no idea what is happening. Does anyone have any suggestions as to why this might be the case?

-EDIT- I do get this error in the LogCat when the application pauses:

04-16 20:09:32.659: ERROR/ActivityManager(66): Reason: Broadcast of Intent { act=android.intent.action.CLOSE_SYSTEM_DIALOGS cmp=com.android.settings/.widget.SettingsAppWidgetProvider (has extras) }

my onPause() code:

    public void onPause() {
    super.onPause();            
    panel.mediaPlayer.pause();
    panel.thread.running=false;
}

Main application thread:

        public void run() {
        running = true;
        while(running) {
            //new Canvas.
            Canvas c = null;
            //Update information
            update();
            //Draw everything to screen
            try {
                //Gets the canvas from the surfaceHolder.
                c = surfaceHolder.lockCanvas(null);
                synchronized(surfaceHolder) {
                    //draw to the canvas                    
                    doDraw(c);
                    try {
                        Thread.sleep(gameSpeed);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }
            } finally {
                if (c != null) {
                    surfaceHolder.unlockCanvasAndPost(c);
                }
            }
        }

mediaPlayer:

            mediaPlayer = new MediaPlayer();
    sublime = context.getResources().openRawResourceFd(R.raw.sublime);

    try {
        mediaPlayer.setLooping(true);
        mediaPlayer.setDataSource(sublime.getFileDescriptor(), sublime.getStartOffset(), sublime.getLength());
        mediaPlayer.prepare();

    } catch (IllegalArgumentException e1) {

    } catch (IllegalStateException e1) {

    } catch (IOException e1) {

    } 
    mediaPlayer.start();
Woodruff answered 16/4, 2011 at 20:15 Comment(1)
Post your onPause() code and anything else related to setting up the objects that it manipulates.Hoptoad
R
2

When you get an "application not responding" error, that means that your activity has taken too long on the main application thread. Identify all the work that you are doing on the main application thread and migrate the slow stuff to a background thread or AsyncTask.

Rhodes answered 16/4, 2011 at 21:20 Comment(1)
I am not really doing too much work in the main application thread. It gets this error when all it should be doing is drawing a single image.Woodruff
W
0

I think I may know what my error is: It is running some unneeded code when onSizeChanged is called on the SurfaceView (which i guess might happen when you pause the Activity and something else comes in to view)... I'll check it out. Thanks everyone!

Woodruff answered 18/4, 2011 at 4:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.