簡體   English   中英

如何在不掛起的情況下導航並恢復我的應用程序?

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

當我在下面運行我的代碼時,一切正常。 當我想返回上一個活動(使用模擬器)時,出現黑屏,然后應用程序關閉。 當我退出應用程序並嘗試恢復它時,我也會出現黑屏。

編碼:

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

我沒有在日志中收到任何錯誤消息,我得到的是大約13條消息,其中指出“暫停所有線程占用:X毫秒”,所以這與我認為的游戲循環線程有關。 不幸的是,我沒有看到我的代碼出了什么問題...

這就是我用來導航的內容。 也許這對您2有用!

Button backpressed = (Button) findViewById(R.id.btBack);
    backpressed.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            onBackPressed();
        }
    });

onBackPressed()只是要返回最后一頁時可以調用的方法。

對我來說是什么樣子:

返回鍵

如果需要上下文,請使用getApplicationContext()或Activity.this。 (我的代碼似乎少了很多錯誤)

回到先前使用的活動時,不會調用onCreate(),而只會調用onResume(),因此,如果將以下幾行移到那里,可能會有所幫助。 drawView = new DrawView(this); setContentView(drawView);

  • 為什么要使用內部類呢? 活動已經是一堂課了。 如果要多次使用該代碼,請單獨使其成為一個類。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM