0

I create a RecyclerView list with a default layout. I then add one new item to the list and the layout updates to show the new item. I then navigate to a previous activity. When I return to the RecyclerView activity I am returned to the generic, default list and my new item in the RecyclerView list is gone.

So how do I return to the RecyclerView and the new item that I created rather than the generic list? Do I need to add some code that says if the size of the adapter is > 0 then don't create a new list use the existing one? And should I be doing the test in the RecyclerView activity or in the adapter? If not, does my issue arise because the adapter is not related somehow to the savedInstanceState of the RecyclerView activity?

Activity:

public class ListContactsActivity extends AppCompatActivity {

private ListContactsAdapter mContactsAdapter;
private RecyclerView mRecyclerView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_recyclerviewlist);

    final List<Contact> mContacts;
    mContacts = new ArrayList<>();

    mRecyclerView = (RecyclerView) findViewById(R.id.cardList);
    mRecyclerView.setLayoutManager(getLayoutManager());
    mRecyclerView.setHasFixedSize(true);
    mContactsAdapter = new ListContactsAdapter(this, mContacts);

    mRecyclerView.setAdapter(mContactsAdapter);
    ...
}

private RecyclerView.LayoutManager getLayoutManager() {
    LinearLayoutManager llm = new LinearLayoutManager(this);
    llm.setOrientation(LinearLayoutManager.VERTICAL);
    return llm;
} 

Adapter:

class ListContactsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
...

@Override
public int getItemCount() {
    return mItems.size();
} 

Toolbar code in Activity:

...
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
toolbar.setNavigationIcon(R.drawable.ic_action_previous_item);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        onBackPressed();
    }
});    
AJW
  • 1,578
  • 3
  • 36
  • 77

1 Answers1

1

When you hit back from an activity you are popping it from the stack i.e the activity object will be destroyed. This is explained in further detail here. The crux of the concept can be understood from this picture.

You can look at this to see how you should recreate your activity - and this to see how you should apply that to a recyclerview.

Community
  • 1
  • 1
Chaitanya Nettem
  • 1,209
  • 2
  • 23
  • 45
  • Thanks I will check out. It's amazing to me that almost every article/blog on RecyclerViews has its focus on creating a list, but none on saving the list. It does no good to create a beautifully crafted and valuable list (e.g., a big list of songs I want to download) and then have the list literally be destroyed if I navigate away from the list with the single press of the back button. Why wouldn't saving state code be a requirement for creating every RecyclerView list just like the other requirements: the adapter, the layoutmanager, onCreateViewHolder, onBindViewHolder and getItemCount? – AJW Feb 17 '16 at 03:09
  • Thanks! I hope you got it sorted out? – Chaitanya Nettem Feb 20 '16 at 18:36
  • Still working on it...trying to solve some Adapter items as well so I have my hands full! – AJW Feb 20 '16 at 18:39
  • Question: I plan to save all of the user's input from the UI in an SQLite database that will then be pushed to the CardViews in the RecyclerView list. Will there ever be a need to save and restore using Parcelable? as you recommend here: http://stackoverflow.com/questions/27816217/how-to-save-recyclerview-scroll-position-with-recyclerview-state-or-no/29166336#29166336 Or will the Parceable not be necessary since views can always be refreshed with the data in the database? – AJW Feb 22 '16 at 04:00