MEM
MEM

Reputation: 25

Return to previous screen from Settings, using the actionbar

Is it possible to change the back button in the actionbar, to go to the previous activity rather than the parent activity?

I'm relatively new to Android, so forgive me if this is a basic question! I've searched for this, but unable to find any answers that match it specifically. I'm using Android Studio, and developing for API 15.

To be clear, please see the image below. I've built a really basic "settings/preferences" activity. I'd like the back arrow in the action bar to go back the previous screen. Currently it only navigates to the parent activity (defined in manifest.xml). Is there any way to do this?

I tried to build this using a preferences fragment, however then I get two issues: (i) the fragment doesn't fill the vertical screen, and (ii) the back arrow in the action bar doesn't work at all. Hence I'm using a preferences activity.

Thanks!

enter image description here

Upvotes: 0

Views: 799

Answers (1)

Neige
Neige

Reputation: 2840

Yes you can !
But you shouldn't => take a look

Otherwise add this to your activity (it should work) :

@Override
public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()){
        case android.R.id.home:
            //DO WHAT YOU WANT WHEN YOU HIT UP BUTTON
            //in your case probably finish()
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

Upvotes: 1

Related Questions