Reputation: 2847
possible duplicate of How to close activity and go back to previous activity in android
The problem is, I add finish() at the end of the method I'm currently on, and the whole app closes.
I want it to return back to the previous screen by pressing the back button on the phone (I don't want to add a back button in the app)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void secondScreen(View v) {
setContentView(R.layout.activity_second_screen);
}
I want to return to main from secondScreen()
Upvotes: 0
Views: 4645
Reputation: 44571
It looks like you only have one Activity
and you are just changing the layout
with setContentView()
. While you could fix this by overriding onBackPressed()
and changing the layout
there, this is not recommended. If you want to separate layout
s then you should have two separate Activities
. So you should create a second Activity
as you did with the first and in the onCreate()
you would have setContentView(R.layout.activity_second_screen);
Then in your, I'm guessing it is, onClick()
you would use an Intent
to go to that second Activity
.
public void secondScreen(View v) {
Intenet i = new Intent(v.getContext(), NextActivityName.class);
startActivity(i);
}
Upvotes: 3