Reputation: 305
Post has been edited
I've programmed an android app as a practice project for learning purposes. I face an issue where when I press on the back button on the main activity after visiting the settings activity and making a change in there (checkbox options), it goes back to the setting activity. If no changes are made in the settings activity, and I go back to the main activity and press back again, it exits fine. If I visit any other activity and then back to the main, press back again it closes fine on one click.
Below you'll see the back button code for both the settings activity and main activity.
user_settings_activity.java (settings activity):
// When the back button is pressed, it goes back to the previous screen.
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
User_Options.java (main activity):
// When the back button is clicked, the user is asked if they want to close the app window or not.
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
Please advise me as to any other code required.
Any help working towards a solution will be appreciated.
Thank you.
SOLUTION
First, thank you everyone who stuck with this question offering responses and comments. I appreciate everyone's help. Now after a bit more tinkering, I've finally managed to work out the solution. Code is below.
Settings Activity:
// For the go back button.
public void go_back_butt(View v){
// Goes to main activity.
Intent main_activity = new Intent(getApplicationContext(), User_Options.class);
main_activity.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
main_activity.putExtra("EXIT", true);
startActivity(main_activity);
finish();
}
And
// When the back button is pressed, it goes back to the previous screen.
@Override
public void onBackPressed() {
super.onBackPressed();
finish();
}
Main Activity:
// When the back button is clicked, the user is asked if they want to close the app window or not.
@Override
public void onBackPressed() {
super.onBackPressed();
if (getIntent().getBooleanExtra("EXIT", false)) {
finish();
}
android.os.Process.killProcess(android.os.Process.myPid());
}
Explanation:
As @chichiangho mentioned, I needed to take an in-depth look at the stack. With my limited knowledge on such matters I did, but managed to figure out that for some reason the settings activity wasn't closing properly as I had originally theorized. So that left me to wonder how I can handle this properly while keeping my saved settings.
Thanks to @kevz and @MFP response, I was able to create a work around. I implemented a "Go Back" button (see code) and used the kill process on main activity to ensure the application "exits" properly. I also passed along an intent from settings activity to main activity to ensure the finish() process takes place.
Thank you all who participated and took the time to post your helpful replies that got me to think more outside of the box and learn new things about android app dev at the same time =).
Upvotes: 2
Views: 2644
Reputation: 2737
Starting SettingActivity from MainActivity on button click-
btnSecond.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, SettingActivity.class));
}
});
No need to finish() MainActvity after starting SettingActivity.
No need to override onKeyDown() or onBackPressed() in SettingActivity.
No matter wt changes u make in setting activity don't override any of above methods and it'll work fine.
Upvotes: 1
Reputation: 57
try do nothing when you close your SettingActivity.
here is your avtivity stack.
at first: MainActivity
start SettingActivity:MainActivity->SettingActivity
start MainActivity in onBackPressed()
:MainActivity->SettingActivity->MainActivity
then finish SettingActivity:MainActivity->MainActivity
see,there are two MainActivities,I think this is why your MainActivity will be looks like restart.
Upvotes: 1
Reputation: 59
Use this ..
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK))
{
finish();
}
return super.onKeyDown(keyCode, event);
}
Upvotes: 1
Reputation: 1141
You can use below code for exit from the app.
@Override
public void onBackPressed() {
super.onBackPressed();
ActivityCompat.finishAffinity(MainActivity.this);
android.os.Process.killProcess(android.os.Process.myPid());
}
Upvotes: 1
Reputation: 4791
Override the onBackPressed function inside your Activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public void onBackPressed() {
super.onBackPressed();
// Call finish() function here
finish();
}
Upvotes: 4