Reputation: 20078
I have created TWO screens which i will paste the code below.
`FirstScreen:` i have a button and when tap/click it will go to `SecondScreen`
`SecondScreen:` i have a button and when tap/click it will go to `FirstScreen`
//code://
public class FirstScreen extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn = (Button)findViewById(R.id.btnPressMe);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Main.this, SecondScreen.class));
}
});
}
}
public class SecondScreen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondscreen);
Button btn = (Button)findViewById(R.id.btnGoToThirdScreen);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//startActivity(new Intent(Main.this, SecondScreen.class));
//HOW DO I GOT TO FIRST SCREEN????
}
});
}
}
if there is a better of way of doing please let me know. - Thanks.
Upvotes: 1
Views: 847
Reputation: 5900
Well you could call finish()
instead of starting a new activity via intent. onDestroy()
is called after finish()
closing the activity.
If you try to go back via intent you would also need the Intent.FLAG_ACTIVITY_CLEAR_TOP
flag set via setFlags()
or you would end up with a lot activities in the back stack if you kept swapping between them.
You should really use the built in back button unless you have a good reason not to.
Upvotes: 1
Reputation: 3910
In the onClick() for the button on the second screen, put just one line:
Finish();
Upvotes: 0
Reputation: 137282
If you want to go back to the first activity, you just need to finish the second one, not create new instance of the first activity, this is done like that:
public void onClick(View v) {
finish();
}
To start the second activity from the first one, you should do:
startActivity(new Intent(FirstScreen.this, SecondScreen.class));
Upvotes: 2