Search code examples
javaandroidsurfaceviewgame-loop

How to navigate back and resume my app without hanging it?


When I run my code below everything works fine. When I want to go back to the previous activity (using the emulator) I get a black screen and then the app shuts down. I also get a black screen when I exit the application and try to resume it.

The code:

package com.example.bono.as3;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import java.io.IOException;
import java.io.InputStream;

public class Main extends Activity {

    DrawView drawView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        drawView = new DrawView(this);
        setContentView(drawView);
    }

    @Override public void onResume(){
        super.onResume();
        drawView.resume();
    }

    @Override public void onPause(){
        super.onPause();
        drawView.pause();
    }

    public class DrawView extends SurfaceView implements Runnable{

        Thread gameloop = new Thread();
        SurfaceHolder surface;
        volatile boolean running = false;
        AssetManager assets = null;
        BitmapFactory.Options options = null;
        Bitmap incect[];
        int frame = 0;

        public DrawView(Context context){
            super(context);
            surface = getHolder();
            assets = context.getAssets();
            options = new BitmapFactory.Options();
            options.inPreferredConfig = Bitmap.Config.ARGB_8888;

            incect = new Bitmap[2];

            try {
                for (int n = 0; n < incect.length; n++){
                    String filename = "incect"+Integer.toString(n+1)+".png";
                    InputStream istream = assets.open(filename);
                    incect[n] = BitmapFactory.decodeStream(istream,null,options);
                    istream.close();
                }
            } catch (IOException e){
                e.printStackTrace();
            }
        }

        public void resume(){
            running = true;
            gameloop = new Thread(this);
            gameloop.start();
        }

        public void pause(){
            running = false;
            while (true){
                try {
                    gameloop.join();
                }
                catch (InterruptedException e){}
            }
        }

        @Override public void run (){
            while (running){
                if (!surface.getSurface().isValid())
                    continue;
                Canvas canvas = surface.lockCanvas();
                canvas.drawColor(Color.rgb(85, 107, 47));
                canvas.drawBitmap(incect[frame], 0, 0, null);

                surface.unlockCanvasAndPost(canvas);

                frame++;

                if (frame > 1) frame = 0;

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e){
                    e.printStackTrace();
                }
            }
        }
    }
}

I dont get any error message in the log, what I do get is about 13 messages saying "suspending all threads took: X ms" so it has something to do with my gameloop Thread I think. Unfortunately I don't see what the problem is in my code...


Solution

  • If you need a context, use getApplicationContext() or Activity.this. (seems less buggy with my code)

    While going back to a previously used activity, onCreate() is not called, only onResume(), so it might help if you move the following lines to there. drawView = new DrawView(this); setContentView(drawView);

    • Why use inner class at all? Activity is a class already. If you want to use the code multiple times, make it a class on its own.