我有一个简单的应用程序,我在其中创建了一个 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();
}
}
这适用于启动和停止应用程序。
问题是当另一个活动出现在这个活动之上时,它的行为方式不稳定(在这个阶段不需要详细信息,因为有时它会崩溃,有时它会与丢失的布局对象一起工作)。
我读到我应该实现onPause()
&onResume()
方法来处理这个问题。我只需要暂停活动或在恢复时恢复它。不需要持久数据。
在这种情况下,我应该在哪些方法中添加什么?
谢谢