Hello all,
I am new to Android programming and I'm trying to figure out how to go back to a previous screen/activity after opening another screen using startActivity. From all the research I've done, using the finish() method should bring up the previous screen just before the current one; however, in my test program (has four screens with a "next" & "back"button), when pressing the back button, it jumps all the way back to the first screen instead of going to the previous screen.
Any help with this would be greatly appreciated. My code is below:
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();
}
}
}