How to get back on previous RecyclerView list onBackPressed

1.1k views Asked by At

When I was going through RecyclerView i got a question. I have a RecyclerView list and on clicking on the list item, I can go to another recyclerView list (Just updated data in same RecyclerView). But when I tried to click on back button, My app closed directly. My question is how can I get back on previous list by onBackPressed method? I tried to save parent list variable in Application context variable and it was a success. But still got same problem when beck was pressed.

1

There are 1 answers

0
Evripidis Drakos On

Have a global var, boolen mItemClicked = false that you set to true when you click a list item. Then overwrite the onBackPressed function like this:

@Override
public void onBackPressed()
{
   if(mItemClicked) {
       //update RecyclerView's data to initial values
       mItemClicked= false;
   }
   else {
       super.onBackPressed(); 
   }
}