AJW
AJW

Reputation: 1649

Android: why is RecyclerView list destroyed after navigating back to the list?

I create a RecyclerView list that shows a default layout. I then add a new item to the list and the layout updates to show the new item (a CardView). I then navigate back to a previous activity. When I return to the RecyclerView activity I am returned to the generic, default list and my new CardView item does not show.

I have the normal onCreate(Bundle savedInstanceState) code in the Activity. It seems like the adapter just creates a new RecyclerView list upon returning to it rather than returning to the previously created list. What am I missing here?

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.setHasFixedSize(true);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    mContactsAdapter = new ListContactsAdapter(this, mContacts);
    mContactsAdapter.setOnItemTapListener(new ListContactsAdapter.OnItemTapListener() {
        @Override
        public void onItemTap(Contact contact, int position) {
            mContactsAdapter.removeItem(contact, position);
        }
    });
    mRecyclerView.setAdapter(mContactsAdapter);
    ...

Adapter:

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

private static class EmptyViewHolder extends RecyclerView.ViewHolder {
    public EmptyViewHolder(View itemView) {
        super(itemView);
    }
}

private class ContactViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    TextView cardBlankText2;

    public ContactViewHolder(View itemView) {
        super(itemView);
        cardBlankText2 = (TextView) itemView.findViewById(R.id.cardBlankText2);
        itemView.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if (mOnItemTapListener != null) {
            Contact contact = mContacts.get(getLayoutPosition());
            mOnItemTapListener.onItemTap(contact, getLayoutPosition());
        }
    }
}

private Context mContext;
private LayoutInflater mLayoutInflater;

private List<Contact> mContacts;
private List<ListItem> mItems;

private OnItemTapListener mOnItemTapListener;

public ListContactsAdapter(Context context, List<Contact> contacts) {
    mContext = context;
    mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mContacts = contacts;
    mItems = buildItemsList();
}

public void setOnItemTapListener(OnItemTapListener listener) {
    mOnItemTapListener = listener;
}

private List<ListItem> buildItemsList() {
    List<ListItem> items = new ArrayList<>();
    if (mContacts.size() > 0) {
        for (Contact contact : mContacts) {
            items.add(new ContactItem(contact));
        }
    } else {
        // when R. list is first created or when the number of cards
        // is deleted until there are zero, show the defaultcard_layout
        // and "Click the + above to get started".
        for (int i=0; i<1; i++) {
            items.add(new EmptyItem());
        }
    }
    return items;
}

public void addItem(Contact contact) {
    if (mContacts.size()==0) {
        // if list is empty 
        // remove empty cards first
        mItems.clear();
        notifyDataSetChanged();
    }

    // add item on the top of the list and scroll to the top position
    mContacts.add(contact);
    mItems.add(new ContactItem(contact));
    notifyItemInserted(0);
}

public void removeItem(Contact contact, int position) {
    mContacts.remove(contact);
    if (mContacts.size()==0) {
        // if no more contacts in list,
        // rebuild from scratch
        mItems.clear();
        mItems.addAll(buildItemsList());
        notifyDataSetChanged();
    } else {
        // else remove one item
        mItems.remove(position);
        notifyItemRemoved(position);
    }
}

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

@Override
public int getItemViewType(int position) {
    return mItems.get(position).getType();
}

@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    if (viewType == ListItem.EMPTY_TYPE) {
        View itemView = mLayoutInflater.inflate(R.layout.defaultcard_layout, parent, false);
        return new EmptyViewHolder(itemView);
    } else {
        View itemView = mLayoutInflater.inflate(R.layout.singlecard_layout, parent, false);
        return new ContactViewHolder(itemView);
    }
}

@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
    int type = getItemViewType(position);
    if (type == ListItem.CONTACT_TYPE) {
        ContactItem item = (ContactItem) mItems.get(position);
        final Contact contact = item.getContact();
        ContactViewHolder holder = (ContactViewHolder) viewHolder;
        holder.cardBlankText2.setText(contact.getName() + " " + contact.getSurname());
    }
}
}

Upvotes: 0

Views: 4650

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317342

When an activity is popped off the stack by navigating back, it doesn't retain any of its views or state. The activity object and all its views and state are destroyed. If you need to somehow save what an activity was doing and need to restore that, you need to write the code to do that.

Upvotes: 3

Related Questions