How to runActivityForResult from an adapter to return the result to a fragment

I have some snippets with custom listviews. They use my own ListAdapter, in which I handle clicks on list items. I need to run another action from this OnClickListener and return some information to the Fragment. i am trying to use

Intent intent=new Intent(context, DataFillerActivity.class); ((Activity) context).startActivityForResult(intent, 3); 

but DataFillerActivity returns the result of MainActivity, not Fragment. so what is the best way to solve this problem? thanks

+7
source share
4 answers

To update your fragment, the only way should be over this. This is because the fragment is designed as modular.

http://developer.android.com/training/basics/fragments/communicating.html

If you run Acitivity for the result, the result will be passed to the Activity that launched the request. Now you can transfer the data to the desired fragment.

+3
source

Make the onActivityResult method the main activity

  @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { //super.onActivityResult(requestCode, resultCode, data); // Check if image is captured successfully super.onActivityResult(requestCode, resultCode, data); for (Fragment fragment : getSupportFragmentManager().getFragments()) { fragment.onActivityResult(requestCode, resultCode, data); } } 

It will pass the result code to its child fragment.

+6
source

According to Steve, the fragment must be modular. However, this means that the message should not go through any activity, but remain within the fragment.

To solve this problem, make sure your ListAdapter has a link to the fragment and uses fragment.startActivityForResult() . Then the result will return to the onActivityResult() fragment.

+5
source

just override onActivityresult in your Activity class and pass the result to the fragment from the action, you can find the fragment, but the id of your tag

+2
source

All Articles