Update ListFragment on completion of operation
I am creating a listfragment and a button that looks like this (Left image)
whenever i click the ADD button a new activity opens and inside that user something is written and clicked on DONE, the activity ends and returns to the ListFragment, but after clicking over DONE, how would i update the ListFragment with new values. i mean where can i write notifyDataSetChanged.
Note: when I click on DONE the data will be stored in the database, and when LoadFragment is loaded it will fetch the data inside of it
I am using Android Support Compatibility Library.
source to share
You have to use StartActivityForResult ().
//place in shared location
int MYACTIVITY_REQUEST_CODE = 101
//start Activity
Intent intent = new Intent(getActivity(), MyActivity.class);
startActivityForResult(intent, MYACTIVITY_REQUEST_CODE);
Then you have to override onActivityResult () in the fragment. The method will be called after the action is closed.
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if ((requestCode == MYACTIVITY_REQUEST_CODE) && (resultCode == Activity.RESULT_OK))
adapter.notifyDataSetChanged()
}
The result code is set in the activity before it completes with:
setResult(Activity.RESULT_OK)
Using requestCode and resultCode is really optional. You only need to use requestCode if you are triggering more than one activity from a fragment. You only need to use resultCode if you need to return different results from the activity.
source to share