James A
James A

Reputation: 135

Why does action bar back button goes back to the top of the recyclerview

How can I change my action bar back button code for it not to go to the top of the recyclerview when someone clicks on it, I want it to be exactly like the back button on my device where when you click it; it goes back to the position you left your recyclerview, thanks!

Upvotes: 0

Views: 457

Answers (1)

Ragnar
Ragnar

Reputation: 255

Had same problem.

My situation: I have recyclerView and when I click one of the items then it go to the new detailActivity, when returning from it with phone back button then the recyclerView is on the same position but when using actionBar back then it jumps on top of the recyclerView.

Goal: That the actionBar back button should work like phone back button

My solution: I deleted these lines in the manifest file. This is the part of DetailActivity

android:parentActivityName=".MainActivity" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.app.MainActivity" />

In the DetailActivity I added this line to onCreate

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

and in onOptionsItemSelected() method I added these lines

 if (id == android.R.id.home) {
            onBackPressed();
        }

Works for me

Upvotes: 2

Related Questions