我有一個簡單的應用程序,其中,我創建了一個SurfaceView類,然後在主活性,我創建的類的一個對象,然後加入此SurfaceView到的相對佈局和內容視圖設置爲相對佈局。恢復的Android活動
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()
方法來處理這個。 我只需要暫停活動或在簡歷上恢復。無需持久數據。
我應該放什麼,在這種情況下,哪些方法?
感謝