我是Android编程的新手,我正在尝试使用startActivity打开另一个屏幕后,如何回到之前的屏幕/活动。从我所做的所有研究中,使用finish()方法应该在当前屏幕之前调出前一个屏幕;但是,在我的测试程序中(有四个屏幕带有“下一个”和“后退”按钮),当按下后退按钮时,它会一直跳回到第一个屏幕而不是前一个屏幕。
对此的任何帮助将不胜感激。我的代码如下:
public class Screen1 extends Activity implements OnClickListener {
private Button next;
private Button quit;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1);
next = (Button) findViewById(R.id.next);
quit = (Button) findViewById(R.id.quit);
next.setOnClickListener(this);
quit.setOnClickListener(this);
}
public void onClick(View view) {
switch(view.getId()) {
case R.id.next: {
startActivity(new Intent(this, Screen2.class));
}
case R.id.quit: {
finish();
}
}
}
}
public class Screen2 extends Activity implements OnClickListener {
private Button next;
private Button back;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen2);
next = (Button) findViewById(R.id.next);
back = (Button) findViewById(R.id.back);
next.setOnClickListener(this);
back.setOnClickListener(this);
}
public void onClick(View view) {
switch(view.getId()) {
case R.id.next: {
startActivity(new Intent(this, Screen3.class));
}
case R.id.back: {
finish();
}
}
}
}
public class Screen3 extends Activity implements OnClickListener {
private Button next;
private Button back;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen3);
next = (Button) findViewById(R.id.next);
back = (Button) findViewById(R.id.back);
next.setOnClickListener(this);
back.setOnClickListener(this);
}
public void onClick(View view) {
switch(view.getId()) {
case R.id.next: {
startActivity(new Intent(this, Screen4.class));
}
case R.id.back: {
finish();
}
}
}
}
public class Screen4 extends Activity implements OnClickListener {
private Button back;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen4);
back = (Button) findViewById(R.id.back);
back.setOnClickListener(this);
}
public void onClick(View view) {
finish();
}
}
}
答案 0 :(得分:0)