I’ve been looking for over a month and I cannot get this resolved anywhere. Whenever I hit the back button my app closes. I’ve searched for finish() I’ve overridden the onBackPressed(), I just cannot seem to solve this issue.
public class Toasty extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_toasty);
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"Here's your toast!", Toast.LENGTH_LONG).show();
setContentView(R.layout.activity_show_toast);
}
});
}
}
This is pretty much all I do. It pops up on the next screen with a text view and a Toast pops up. Then I press that back button and the app exits when all I want to do is reset so that the button can be pressed again.
“Not returning to previous view”
This is because when you press the back button it doesn’t leave a view, it leaves an Activity.
Hence if you have one Activity it will exit the application, no matter how many times you change the view.
To change the behaviour either:
A. Create a new Activity with your second view and go to it via an Intent
NOTE: ActivityTwo will be an activity containing R.layout.activity_toasty as its view
B. Override the back button as below