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();