简体   繁体   English

恢复Android活动

[英]Resuming Android Activity

I have a simple application, where I created a SurfaceView class, then in the main activity, I created an object of the class, then added this SurfaceView to a relative layout, and set the content view to the relative layout. 我有一个简单的应用程序,在其中创建了SurfaceView类,然后在主活动中创建了该类的对象,然后将此SurfaceView添加到相对布局中,并将内容视图设置为相对布局。

package com.my.game;
import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdRequest.ErrorCode;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;

public class ShootLetters extends Activity {

    private Panel panel;
    private int newWidth;
    private int newHeight;

    private AdView adView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bitmap bg = BitmapFactory.decodeResource(this.getResources(),
                R.drawable.shooting_background);
        ;

        int width = bg.getWidth();
        int height = bg.getHeight();

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        newWidth = metrics.widthPixels;

        newHeight = metrics.heightPixels;
        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;
        // CREATE A MATRIX FOR THE MANIPULATION
        Matrix matrix = new Matrix();
        // RESIZE THE BIT MAP
        matrix.postScale(scaleWidth, scaleHeight);

        Bitmap resizedBackGround = Bitmap.createBitmap(bg, 0, 0, width, height,
                matrix, false);

        Window win = getWindow();
        Drawable d = new BitmapDrawable(resizedBackGround);
        win.setBackgroundDrawable(d);

        panel = new Panel(this, newWidth, newHeight);

        panel.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    panel.setClickX(event.getX());
                    panel.setClickY(event.getY());
                }
                return true;
            }

        });

        adView = new AdView(this, AdSize.BANNER, "admobidxxx222");

        RelativeLayout rl = new RelativeLayout(this);
        rl.addView(panel);
        rl.addView(adView);

        setContentView(rl);

        // Initiate a generic request to load it with an ad
        adView.loadAd(new AdRequest());
    }

    @Override
    public void onDestroy() {
        if (adView != null) {
            adView.destroy();
        }
        panel.stopThread();
        panel = null;

        super.onDestroy();
    }

}

This works fine for starting and stopping the application. 这对于启动和停止应用程序来说很好。

The problem is when another activity comes on top of this one, it behaves in an unstable way (no need for details at this stage as sometimes it crashes and sometimes it works with missing layout objects). 问题在于,当另一个活动位于此活动之上时,它的行为会不稳定(此阶段不需要详细信息,因为有时它会崩溃,有时会与缺少的布局对象一起工作)。

I read that I should implement onPause() & onResume() methods to handle this. 我读到应该实现onPause()onResume()方法来处理此问题。 I only need pause the activity or resume it on resume. 我只需要暂停活动或在恢复时恢复活动即可。 No persistent data required. 不需要持久性数据。

What should I put in which methods in this case? 在这种情况下,我应该使用哪种方法?

Thanks 谢谢

I guess its your Panel that is the surfaceview? 我想您的Panel就是SurfaceView吗? I see you are doing something with its thread in onDestroy but that is not sufficient. 我看到您正在使用onDestroy中的线程进行操作,但这还不够。 You had a correct hunch about implementing onPause() and onResume() Now my idea(without being able to see all of your code) is that you are not dealing with the thread correctly. 您对实现onPause()onResume()有正确的直觉,现在我的想法(无法看到所有代码)是您没有正确处理线程。 In your Panel class which extends surfaceview you should have something like these two functions: 在扩展曲面视图的Panel类中,您应该具有以下两个功能:

    public void pause(){
        Loop = false;
        while (true){
            try{
                panelThread.join();//end it
            }catch (InterruptedException e){
                e.printStackTrace();
            }
            break;
        }
        panelThread = null;
    }
    public void resume(){
        Loop = true;
        panelThread = new Thread(this);
        panelThread.start();
    }

Ignore the Loop part if you are not using a loop in your run() method. 如果您不在run()方法中使用循环,请忽略循环部分。

Then, in your activity's onPause() and onResume() methods you call panel.pause() and panel.resume() respectively. 然后,在活动的onPause()onResume()方法中,分别调用panel.pause()panel.resume() This makes sure that the surface view thread is being handled in a safe way. 这样可以确保以安全的方式处理曲面线程。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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