knightcube
knightcube

Reputation: 159

How do I pass a RecyclerView item in the second Activity to the previous Activity in Android?

I have 2 activities - A and B

A- Detail Activity where details get updated

B- A Search Activity or Fragment where the user selects an item from a list of items and the selected item is reflected in Activity A

Can anyone suggest a good and efficient way to achieve this functionality?

Upvotes: 0

Views: 662

Answers (5)

akshay_shahane
akshay_shahane

Reputation: 4643

public interface OnItemClickListener {
    void onItemClick(ContentItem item);
}

Above code in your adapter

private final List<ContentItem> items;
private final OnItemClickListener listener;

public ContentAdapter(List<ContentItem> items, OnItemClickListener listener) {
    this.items = items;
    this.listener = listener;
}



@Override public void onBindViewHolder(ViewHolder holder, int position) {
    holder.bind(items.get(position), listener);
}


public void bind(final ContentItem item, final OnItemClickListener listener) {
    ...
    itemView.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            listener.onItemClick(item);
        }
    });
}


// in your Activity 

recycler.setAdapter(new ContentAdapter(items, new ContentAdapter.OnItemClickListener() {
    @Override public void onItemClick(ContentItem item) {
        Intent i = new Intent();
        i.putExtra("key","value");
        startActivity(i)
    }
}));

read more

Upvotes: 2

Mehul Vasa
Mehul Vasa

Reputation: 51

You can pass the data from one Activity to another using intent. Just put the data in bundle or extra when you are calling an intent to navigate through one Activity to another. Like this

Intent asd = new Intent(getApplicationContext, ActivityA.class);
asd.putExtra(strName, STRING_I_NEED);
startActitvity(asd);

And to get data in ActivityA,

Bundle extras = getIntent().getExtras();
if(extras == null) {
     newString= null;
} else {
     newString= extras.getString("STRING_I_NEED");
}

Upvotes: 0

Keyur Lakhani
Keyur Lakhani

Reputation: 4371

There are two way of doing this

  1. If you wanna just need to pass the data from A to B activity and move forward then you can use the serializable object and pass it along with the intent and afterwards if you return back on the A again then in onResume method you can call the datasource again for the data. Link

  2. You can startActivityForResult(); in activity A and that will starts B Activity and after returning from the B it will automatically trigger onActivityResult(); Link

Upvotes: 0

p.mathew13
p.mathew13

Reputation: 921

If you are using B as a fragment you can use an interface to communicate between your fragment and activity.Any changes in the fragment can be reflected back in your parent activity.

Upvotes: 0

Sakura Fukuyoshi
Sakura Fukuyoshi

Reputation: 1471

How about the onActivityResult?

Check this one http://android-er.blogspot.com/2011/08/return-result-to-onactivityresult.html

Upvotes: 0

Related Questions