Reputation: 978
I have been struggling with this case for a long time now, any help would be appreciated.
I have a main activity with 3 fragments (in other words I have an activity with three tabs). On the third fragment I have a button starts a new activity through intent. That new activity is a QR scanner so it basically calls the camera and waits until camera finds appropriate and valid QR code. How do I get the result back to my fragment?
My button's onClick method which located in onCreate method in Fragment3 which extends Fragment class:
Intent intent = new Intent(getActivity(), ZBarScannerActivity.class);
startActivityForResult(intent, 1);
My MainActivity's onActivityResult method:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_CANCELED)
{
if (resultCode == Activity.RESULT_OK)
{
String result = data.getStringExtra(ZBarConstants.SCAN_RESULT);
}
}
}
From this on I cannot figure out how to get this result back to Fragment3's onActivityResult method. Thank you in advance.
Upvotes: 2
Views: 276
Reputation: 11357
FragmentManager.BackStackEntry backEntry=getFragmentManager().getBackStackEntryAt(getActivity().getFragmentManager().getBackStackEntryCount()-1);
String str=backEntry.getName();
Fragment fragment=getFragmentManager().findFragmentByTag(str);
This will give you the latest instance of the fragment. Make sure that you added your backstack entry like this.
fragmentTransaction.addToBackStack(someName);
Or while adding the fragment you can provide a tag. Like below
public abstract FragmentTransaction add (int containerViewId, Fragment fragment, String tag)
Upvotes: 1