Question

I have 2 activities : MainActivity and SettingActivity.

Initially the App is launched to MainActivity. I start the menu by:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.action_settings:
            Intent settingActivity = new Intent(MainActivity.this, SettingActivity.class);
            startActivity(settingActivity);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Then, there is a Save button is SettingActivity. In the onClick function, it will be brought back to MainActivity by:

Intent mainActivity = new Intent(SettingActivity.this, MainActivity.class);
startActivity(mainActivity);

1st question, is this flow correct?

Next, during I go to SettingActivity and click Save button to go back to MainActivity, when I press Back button, it goes back to SettingActivity. I expect the Back button will back to close the App (no need to kill the App) instead of going back to Settings. So, my 2nd question, how can I create my expected behavior?

Side Note: Quite new to Android, in iOS's concept using UINavigationController, it will go back to previous back when the Back button on UINavigationBar.

Was it helpful?

Solution 2

Instead of re-launching the MainActivity using a startActivity() call you should instead call finish() on the SettingsActivity in your save method, once you have persisted your various changes.

The default behaviour will then take you back to the last Activity that was running, in this situation your MainActivity.

A more complete example;

// MainActivity
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.action_settings:
            Intent settingActivity = new Intent(MainActivity.this, SettingActivity.class);
            startActivity(settingActivity);
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

@Override
protected void onResume() {
    // This will be called after you finish SettingsActivity.
}

// SettingsActivity
private void save() {
    // Save your current options

    finish();
}

OTHER TIPS

Just call finish(); from SettingActivity instead of

Intent mainActivity = new Intent(SettingActivity.this, MainActivity.class); startActivity(mainActivity);

you are just starting the main activity again from the setting activity. That's why the settingactivity is set as the previous activity in the activity stack. There are several options to achieve what you want. But I would choose to finish the Setting activity when the job is done. So you would automatically go to the Main Activity.

Your back stack when going to SettingsActivity is:

MainActivity
SettingsActivity

So when you press SAVE and create another intent to MainActivity, your SettingsActivity goes to background, that's why when you press BACK, it goes back to Settings, because SettingsActivity is resumed. This is a normal Android behaviour.

What you want to do is to finish SettingsActivity, so when you press SAVE button in Settings, just go back to the previous (MainActivity) by calling:

finish();

instead of Intent to MainActivity.

Read the documentation here and here.

Then, there is a Save button is SettingActivity. In the onClick function, it will be brought back to MainActivity by:

Intent mainActivity = new Intent(SettingActivity.this, MainActivity.class);
startActivity(mainActivity);"

Please replace this

Intent mainActivity = new Intent(SettingActivity.this, MainActivity.class);
startActivity(mainActivity);

by

finish();

You can use ActivityGroup activity to control your activities. Whenever you inflate an activity, you push the activity to the stack and whenever you press back, you pop the activity from the stack.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top