0

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?

KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
knightcube
  • 139
  • 1
  • 13
  • https://developer.android.com/reference/android/support/v7/widget/RecyclerView.html and find findViewHolderforLayoutPosition, findViewHolderForAdapterPosition – Salman500 Aug 14 '17 at 06:11
  • while starting Actviity B use startActivityForResult() and get your result back in Actviity A. – Pallavi Tapkir Aug 14 '17 at 06:12

5 Answers5

2
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

KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
akshay_shahane
  • 4,423
  • 2
  • 17
  • 30
0

How about the onActivityResult?

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

Sakura Fukuyoshi
  • 1,461
  • 1
  • 10
  • 14
0

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.

p.mathew13
  • 920
  • 8
  • 16
0

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");
}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42
Mehul Vasa
  • 51
  • 9
0

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

Keyur Lakhani
  • 4,321
  • 1
  • 24
  • 35