我在我的游戏中使用了surfaceview。 当游戏完成后,它将通过此意图自动启动其他活动(得分活动):
mContext = getContext();
Intent intent = new Intent(mContext, ScoreScreen.class);
mContext.startActivity(intent);
它工作正常,但是当我想关闭那个得分活动然后回到surfaceview(“再试一次”按钮)时有一个问题。
我使用此代码完成分数活动:
tryagain.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
但它不想回到surfaceview
帮帮我......
抱歉我的英文不好这是我的SurfaceviewScreen:
public class SurfaceScreen extends Activity {
OurView v;
int level =1, lives = 3, score=0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
v = new OurView(this);
v.setKeepScreenOn(true);
setContentView(v);
}
@Override
protected void onPause() {
super.onPause();
v.pause();
}
@Override
protected void onResume() {
super.onResume();
v.resume();
}
public class OurView extends SurfaceView implements Runnable {
Thread t = null;
SurfaceHolder holder;
boolean isItOK = false;
boolean spriteLoaded = false;
private Context mContext;
Paint paint = new Paint();
public OurView(Context context) {
super(context);
this.mContext = context;
mContext = getContext();
holder = getHolder();
}
@Override
public void run() {
while (isItOK) {
if (!holder.getSurface().isValid()) {
continue;
}
update();
Canvas c = holder.lockCanvas();
onDraw(c);
holder.unlockCanvasAndPost(c);
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void pause() {
isItOK = false;
while (true) {
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
t = null;
}
public void resume() {
isItOK = true;
t = new Thread(this);
t.start();
}
protected void update(){
if(lives<1){
Intent i = new Intent(mContext, gameover.class);
i.putExtra("msg", "LOSE");
mContext.startActivity(i);
}
//updating
}
@Override
protected void onDraw(Canvas canvas) {
//drawing
}
}
}
}
这是我的ScoreScreen代码:
public class ScoreScreen extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.scorescreen);
TextView txtResult = (TextView)findViewById(R.id.scorescreen_result);
Button tryagain = (Button)findViewById(R.id.scorescreen_tryagain);
Intent i = this.getIntent();
txtResult.setText(i.getStringExtra("msg"));
tryagain.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
}
答案 0 :(得分:0)
据我所知,您应该尝试
ScoreActivity.this.finish();