Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Back Button Doesn't Return to Previous Activity

I have an app that has two activities: MainActivity and SettingsActivity. The MainActivity has a menu with a single Settings menu item. When this menu item is clicked, it launches the SettingsActivity with an intent. After the activity starts up, I click the back button in the top left corner and nothing happens. I assumed since I started the activity using an intent, the activity stack would be managed automatically. I want to return to the MainActivity. Am I wrong in this assumption?

MainActivity.onMenuItemSelected

public boolean onMenuItemSelected(int featureId, MenuItem item) {
    int itemID = item.getItemId();

    if(itemID == R.id.settings) {
        Intent intent = new Intent(this, SettingsActivity.class);
        startActivity(intent);
    }

    return true;
}

SettingsActivity

public class SettingsActivity extends PreferenceActivity {

    public static final String TEST = "test";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}
like image 275
Dan Taylor Avatar asked Jul 14 '13 20:07

Dan Taylor


People also ask

How to go back to previous activity in Android Studio?

add back button to back to previous activity android studio can return to previous activity how to apply go back to an activity from an activity on button click android start activity and go back to previous pip go back to previous activity android

How to restart an activity when back button is pressed?

@Override public void onRestart () { super.onRestart (); //When BACK BUTTON is pressed, the activity on the stack is restarted //Do what you want on the refresh procedure here } You could code what you want to do when the Activity is restarted (called again from the event 'back button pressed') inside onRestart ().

How to create a new activity in Android Studio?

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main.xml. In the above code, we have given text view, when the user click on text view, it will open new activity.


1 Answers

Inside your SettingsActivity you need to override onOptionsItemSelectedto enable the back button on top left corner of Action Bar for going back. It does not know by itself that what it needs to do on click. Inside the case android.R.id.home you can just call finish(). This will finish your current activity and you will go back to MainActivity which started it. Eg:

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

Just for completeness, to enable the home button, you may add the following in your onCreate() of SettingsActivity:

getActionBar().setDisplayHomeAsUpEnabled(true);

As per docs of setDisplayHomeAsUpEnabled()

It is to show the user that selecting home will return one level up rather than to the top level of the app.

Hope this helps.

like image 118
Shobhit Puri Avatar answered Oct 12 '22 23:10

Shobhit Puri