return value to fragment from activity android

匿名 (未验证) 提交于 2019-12-03 02:35:01

问题:

I have Fragment(F) which calls the activity(A). On press of button(B) in activity(A) the activity must return selected arraylist value back to fragment(F) and finish activity(A)... is this possible?

I know from Activity you can send data with intent as:

Bundle bundle = new Bundle(); bundle.putString("key", "value"); // set Fragmentclass Arguments Fragmentclass fragmentobj = new Fragmentclass(); fragmentobj.setArguments(bundle); 

in Fragment onCreateView method:

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,         Bundle savedInstanceState) {     String strtext = getArguments().getString("key");         return inflater.inflate(R.layout.fragment, container, false); } 

but this works for calling new fragment not working with same fragment.

My question: is it possible for called Activity to return value to caller fragment?

public class HomeFragment extends Fragment{     Button attachment;     String strtext = "check";        @Override     public void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);             setHasOptionsMenu(true);     }         @Override     public View onCreateView(LayoutInflater inflater, ViewGroup container,             Bundle savedInstanceState) {         View rootView = inflater.inflate(R.layout.home_fragment, null);              attachment = (Button) rootView                 .findViewById(R.id.btn_activity);                Toast.makeText(getActivity(),                 "Key= " + strtext,                 Toast.LENGTH_LONG).show();         attachment.setOnClickListener(new OnClickListener() {              @Override             public void onClick(View arg0) {                 // TODO Auto-generated method stub                  Intent myIntent = new Intent(getActivity(), AndroidCustomGalleryActivity.class);                 getActivity().startActivityForResult(myIntent,999);               }         });          return rootView;      }         public void onActivityResult(int requestCode, int resultCode, Intent data) {                  if(requestCode == 999){         strtext = getArguments().getString("key");           Toast.makeText(getActivity(),                 "Key= " + strtext,                 Toast.LENGTH_LONG).show();         }     } } 

In Called Activity

Bundle bundle = new Bundle(); bundle.putString("key", "value"); HomeFragment fragmentobj = new HomeFragment(); fragmentobj.setArguments(bundle); finish(); 

回答1:

In your fragment(F) call the startActivityForResult() method to start the activity A, then override onActivityResult() method in the fragment.

So when the activity A finishes you can get get the result bundle in onActivityResult() method.

EDIT

In your fragment onClick() call the activity like this

Intent myIintent=new Intent(getActivity(), AndroidCustomGalleryActivity.class);     startActivityForResult(myIintent, 999); 

And in your activity

Intent intent = getIntent();     Bundle bundle = new Bundle();     bundle.putString("key", "value");     intent.putExtras(bundle);     setResult(RESULT_OK, intent);     finish(); 

Now you will get the result in onActivityResult() method of the fragment.

EDIT2

In your fragment onActivityResult() method you have to get the data like this...

Bundle extras = data.getExtras();     strtext =   extras.getString("key"); 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!