There is a button and list view inside the viewpager fragment of an activity. When button clicked another activity opens. When the data saved in this activity will reflect the data on the list item of the fragment.

There is a button and list view inside the viewpager fragment of an activity. When button clicked another activity opens. When the data saved in this activity will reflect the data on the list item of the fragment.





We have activity1 inside that a fragment called DetailsFragment , and another activity called activity2.

1. Activity 1

Create a listener in this activity

private onDataSendListener mDataSendListnr;
public Interface onDataSendListener {

void onDataSend();

}
public void setDataSendListener(onDataSendListener lst)
{
this.mDataSendListnr=lst;
}

2. Activity 2

Send back the result code to the called activity

setResult(RESULT_OK, output);
finish();

3. Activity 1

check the result is from the particular activity in onActivityResult

if(resultCode=1001)
{
    // call the listener method
      mDataSendListnr.onDataSend();
}

4. DetailsFragment

Implement the listener as

DetailFragment implements Activity1.onDataSendListener
{

@Override
public void onDataSend() 
{
-add data to list
-set adapter
}

..............
............
.........

onAddButtonClck()
{
getActivity().startActivityForResult(Activity2 intent, 1001);
}
}

This will work fine without any errors !!!

Comments