How to make onActivityResult get called on nested fragment from viewpager - android

This is a famous problem of getting result from activity to a nested fragment, the activity could send the result to only the Fragment that has been attached directly to Activity but not the nested one. That's the reason why onActivityResult of nested fragment would never been called no matter what.
In my case i have one activity that contains one fragment which has a viewPager with bunch of fragments.
In one of this fragments of the viewPager i try to start a camera intent, the result returned by this intent is never sent to the last fragment, i tried severals answers that i found but none of them is robust, one of the solutions was to create an EventBus object to diffuse the resultCode from activity to all the fragments but it doesn't work for this case, any help??

I might have a working solution for you since I used to be in a very similar situation.
In short terms I stopped relying on Fragment's onActivityResult() and implemented a working logic myself.
The only onActivityResult() you should trust is the one of your Activity.
You may create an interfacelike this one
public interface ActivityResultDispatcher {
// Pass anything you want, Bundle, Intent, ecc...
// For the sake of simplicity I'm using Bundle...
public void dispatchResultData(final Bundle data);
}
and have your Fragment classes implement it.
The communication between your Activity and your primary Fragmentshould be very easy that you can do something like this (in your Activity):
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
final Bundle bundle = new Bundle();
// put data inside Bundle
this.primaryFrament.dispatchResultData(bundle);
}
Now, your primary Fragment is the one that holds the ViewPager with the other Frament classes.
So in your primary Fragment you will do:
#Override public void dispatchResultData(final Bundle data) {
for (int i = 0; i < MyViewPager.NUM_FRAGMENTS; i++) {
final ActivityResultDispatcher subFragment = (ActivityResultDispatcher) this.retrieveFragment(i);
// some fragments in ViewPager may be dynamically deleted
if (subFragment != null) {
subFragment.dispatchResultData(data);
}
}
}
The function retrieveFragment(final int position) is a very cozy piece of code I had found on the web.
An implementation for a ViewPager works like this:
public Fragment retrieveFragment(final int position) {
return this.fragmentManager.findFragmentByTag("android:switcher:" + R.id.my_pager_id + ":" + position);
}
All your active Fragment classes in the ViewPager will receive the Bundle and you can decide what to do with it.
You may pass more information to the dispatcher, such as
public void dispatchResultData(final Bundle data, final int requestCode);
and have your Fragment decide if it should answer to the function call.
You have basically built a complete fully-working onActivityResult() utility and can be sure that all of your Fragments (if instantiated) will receive the result.

Related

Start an Activity form a Fragment

I searched in site and there were similar questions as mine but none of theme were not my answer
look at this picture:
so it is clear that i want to start CrimeActivity by sending an intent from CrimeListFragment + an extra in its intent
the book that i read for android programming its author said:
Starting an activity from a fragment works nearly the same as starting an activity from another activity.
You call the Fragment.startActivity(Intent) method, which calls the corresponding Activity
method behind the scenes
CrimeListFragment.java :
public void onListItemClick(ListView l, View v, int position, long id) {
// Get the Crime from the adapter
Crime c = ((CrimeAdapter)getListAdapter()).getItem(position);
// Start CrimeActivity
Intent i = new Intent(getActivity(), CrimeActivity.class);
i.putExtra(CrimeFragment.EXTRA_CRIME_ID, c.getId());
startActivity(i);
}
the second part is now retrieving the intent and its extra and the author said about that:
There are two ways a fragment can access data in its activity’s intent: an easy, direct shortcut and a
complex, flexible implementation. First, you are going to try out the shortcut. Then you will implement
the complex and flexible solution that involves fragment arguments.
and my problem is about the first way, the shortcut
In the shortcut, CrimeFragment will simply use the getActivity() method to access the
CrimeActivity’s intent directly. Return to CrimeFragment and add the key for the extra. Then, in
onCreate(Bundle), retrieve the extra from CrimeActivity’s intent and use it to fetch the Crime
CrimeFragment.java :
public class CrimeFragment extends Fragment {
public static final String EXTRA_CRIME_ID =
"com.bignerdranch.android.criminalintent.crime_id";
private Crime mCrime;
...
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mCrime = new Crime();
UUID crimeId = (UUID)getActivity().getIntent()
.getSerializableExtra(EXTRA_CRIME_ID);
mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);
}
The downside to direct retrieval
Having the fragment access the intent that belongs to the hosting activity makes for simple code.
However, it costs you the encapsulation of your fragment. CrimeFragment is no longer a reusable
building block because it expects that it will always be hosted by an activity whose Intent defines an
extra named EXTRA_CRIME_ID.
This may be a reasonable expectation on CrimeFragment’s part, but it
means that CrimeFragment, as currently written, cannot be used with
just any activity.
My question and problem is the last sentence, why this Fragment (CrimeFragment) cannot be used with just any Activity???
The author explains it. Your CrimeFragment, in its onCreate() method, gets its hosting activity (through getActivity()) and then attempts to get an UUID from the Intent used to start that Activity.
This means that any activity containing your CrimeFragment now has to obey this rule, i.e. its intent should have (in it) an extra defined by the name EXTRA_CRIME_ID. If that activity does not comply, you'll see an exception being thrown in CrimeFragment's onCreate().
Try having this fragment in a new activity created by yourself to see what happens.
retrieval in onActivityCreated()
#Override
public void onActivityCreated(Bundle savedInstanceState) {
if (savedInstanceState != null) {
....
}
else {
UUID crimeId = (UUID)getActivity().getIntent().getSerializableExtra(EXTRA_CRIME_ID);
}
}

OnActivityResult when having GUI parts outside Fragment

I have a two pane fragment design with the detail fragment dealing with the item clicked on the leftmost, listfragment.
Since I have many different list items, they have different GUI's each one of them, I want to put the handling of the GUI parts in different separate classes.
I'm using reflections to get the code slimmed inside the ItemDetailFragment:(code below is stripped down)
*//get class gui_handler for the object and get its constructor:*
Constructor<?> ctor = mListItem.getmGuiHandler().getConstructor(View.class);
*//create an object of the gui_handler class, pass the rootView as arg:*
gui_handler_base handlerObject = (gui_handler_base) ctor.newInstance(root);
*//run setup-method containing findViewById() and more GUI related stuff:*
handlerObject.setupGUI(mListItem, getActivity());
In setupGUI:
public void setupGUI(MyListItem item, final Activity activity) {
buttonRun.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
final String FILENAME = "XXX";
Intent I = new Intent(activity,KTActivity.class);
Bundle b = new Bundle();
b.putSerializable("FileName",FILENAME);
I.putExtras(b);
activity.startActivityForResult(I,1);
return;
}
});
#Override
public void onActivityResult(int requestCode, int resultCode,
Intent data) {
// I WANT TO END UP HERE WHEN THE ACTIVITY RETURNS;
}
I have one onActivityResult method in the gui_handler_base class and one in the ItemDetailFragment, but none of them are called.
How can I do this??
Your question is quite ambiguous. Why are you using onActivityResult? activity.startActivityForResult(I,1); why? WHy not just implement the onclicklistener class in your fragment class and be done with it? It seems like in trying to reduce code you created more headaches for yourself. Also take out numbers in your code and use constants. You have to call the intent and use the intent in the same fragment. Further more why are you extracting out the GUI functionality from the fragments? You shouldn't be. even if you are creating custom functionality, you would still deal with a fragment's functionality in the fragment.
//if you want to try this… normally when receiving a camera intent this is what you can do.
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case yourResultCode:
doSomethingWithYourResult;
break;
]

ViewPager + Fragment + saveInstanceState

I have a simple Activity containing a ViewPager, which displays Fragments.
My Activity should display information about a football league, and each fragment displays information like livescroes/matchdays, tables, etc.
The Intent with which I start the Activity, contains the league id.
And each Fragment needs this league id to load the correct data.
So my FragmentPagerAdapter looks like this
public class LeaguePagerAdapter extends FragmentPagerAdapter {
private String leagueId;
public LeaguePagerAdapter(FragmentManager fm, String leagueId) {
super(fm);
this.leagueId = leagueId;
}
#Override
public Fragment getItem(int pos) {
if (pos == 0){
return TableFragment.newInstance(leagueId);
} else {
return MatchdayFragment.newInstance(leagueId);
}
}
}
The TableFragment looks like this ( the matchday fragment looks similar):
public class TableFragment extends PullToRefreshListViewAdFragment {
private String leagueId;
public static TableFragment newInstance(String leagueId) {
TableFragment t = new TableFragment();
t.leagueId = leagueId;
return t;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Setup UI and load data
}
}
Sometimes the leagueId is null. I see the exceptions in the crash logs (crittercism). But Im asking my self why. It seems to me, that the problem is when the activity has been destroyed in the background and reconstructed if (for instance) the user uses the multitasking button to switch to my app.
So as far as I know, the original Intent will be stored internally by Android itself if the Activity has been destoryed. Therefore I have not implemented any onSaveInstanceState() in my activity nor in the fragment. In my activity I read the Intent Extra to retrieve the leagueId. This works fine, also on restoring the activity. I have assumed that by recreating the activity, a new LeaguePagerAdapter will be created and all fragments will also be new created.
Is that correct? Or does the "old" fragment instance will be restored and hence the leagueId is null (because the fragment has not stored the leagueId in Fragments onSaveInstanceState method?).
Is there a way to test such lifecycle things
The reason it is null is because the system restores the Fragment with the default constructor. Here's what the documents say:
Every fragment must have an empty constructor, so it can be instantiated when restoring its activity's state. It is strongly recommended that subclasses do not have other constructors with parameters, since these constructors will not be called when the fragment is re-instantiated; instead, arguments can be supplied by the caller with setArguments(Bundle) and later retrieved by the Fragment with getArguments().
edit: also, take a look at this: Fragment's onSaveInstanceState() is never called
edit: To further add on, you are creating your Fragment with your newInstance(String) method. If your Fragment is killed by Android, it uses the default constructor and so your leagueId variable won't be set. Try using setArguments/getArguments to pass the value into your Fragment instead.

PreferenceFragment doesn't get onActivityResult call from in app billing request

I have a preference screen that presents the user with a checkbox to disable ads. When the user clicks this for the first time, they are presented with an In App Billing purchase option to disable the ads.
The issue I'm facing here is that I can't see any way to get the onActivityResult callback into the fragment.
So I have a PreferenceActivity loading a PreferenceFragment (of which I can't seem to get a reference). In App Billing requires a call to startIntentSenderForResult, which Fragments don't have, only Activities.
When I launch the purchase flow with startIntentSenderForResult, the Activity's onActivityResult gets called, but I need this in the fragment.
Because I loaded the PreferenceFragment into the PreferenceActivity with the following, I don't think I can get a reference to the Fragment to pass the call down.
#Override
public void onBuildHeaders(List<Header> target) {
loadHeadersFromResource(R.layout.preferences_headers, target);
}
#Override
public Intent getIntent() {
final Intent modIntent = new Intent(super.getIntent());
modIntent.putExtra(EXTRA_SHOW_FRAGMENT, SyncPreferencesFragment.class.getName());
modIntent.putExtra(EXTRA_NO_HEADERS, true);
return modIntent;
}
What am I missing here? I dont' want to split up all of my purchase logic, so how do I get my Fragment to get the onActivityForResult call?
The original start request must come from the Fragment in order for Android to deliver the result back to the same Fragment. If the Activity starts the request, the result doesn't inherently get handed to every Fragment that may be attached to the manager. In addition to this, you have to ensure that if onActivityResult() is overridden in the top-level Activity, that you call super.onActivityResult() as well, or the message won't get delivered to the Fragment.
The problem with IAB is your given a PendingIntent instead of a standard Intent to fire, and there is no method on Fragment to trigger the initial action even if you can move your code there. To keep things as they are, you may have to do a bit of a swizzle. One thing you could do is make use of a custom interface inside the onAttach() method of your Fragment and use it to allow the Fragment to hand itself up to the Activity. Something like:
public class SyncPreferencesActivity extends PreferenceActivity
implements SyncPreferencesFragment.OnAttachCallback {
SyncPreferencesFragment mTargetFragment;
#Override
public void onAttachSyncFragment(SyncPreferencesFragment fragment) {
mTargetFragment = fragment;
}
}
...with some additions to the corresponding Fragment...
public class SyncPreferencesFragment extends PreferenceFragment {
public interface OnAttachCallback {
public void onAttachSyncFragment(SyncPreferencesFragment fragment);
}
#Override
public void onAttach(Activity activity) {
try {
OnAttachCallback callback = (OnAttachCallback) activity;
callback.onAttachSyncFragment(this);
} catch (ClassCastException e) {
throw new ClassCastException("You Forgot to Implement the Callback!");
}
}
}
With something like this, at least you have a reference to the instance so you can forward the result or anything else that might be necessary. If you want to be super clean, you could also implement a matching "detach" that clears the reference in the Activity.
Here is a simpler way that I am using to get a reference to a Preference Fragment from a Preference Activity:
private WeakReference<GeneralPreferenceFragment> mGeneralFragment;
#Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
// "id" might be arbitrary and "tag" can be null
Log.d(TAG, "onAttachFragment: "+fragment.getId()+","+fragment.getTag());
// however class can be used to identify different fragments
Log.d(TAG, "onAttachFragment: "+fragment.getClass()+","+(fragment instanceof GeneralPreferenceFragment));
if(fragment instanceof GeneralPreferenceFragment) {
mGeneralFragment=new WeakReference<>((GeneralPreferenceFragment)fragment);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Fragment f=mGeneralFragment.get();
if(f!=null) {
// can now use findPreference, etc
}
}
This method is convenient because it doesn't require the use of interfaces or modifications to the fragment classes.

Custom View calling startActivityForResult

I created custom compound view where I incorporate functionality to take pictures.
I'm calling it like this (from view):
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
((Activity)mContext).startActivityForResult(intent, index);
This part works good. What I don't know how to do is how do I implement onActivityResult inside my custom view?
Or should I catch this inside Activity and than re-route into my view? Doesn't look like very nice solution..
You actually can do it like this:
#Override
public void onClick(View v) {
final FragmentManager fm = ((FragmentActivity) getContext()).getSupportFragmentManager();
Fragment auxiliary = new Fragment() {
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
//DO WHATEVER YOU NEED
super.onActivityResult(requestCode, resultCode, data);
fm.beginTransaction().remove(this).commit();
}
};
fm.beginTransaction().add(auxiliary, "FRAGMENT_TAG").commit();
fm.executePendingTransactions();
auxiliary.startActivityForResult(new Intent(getContext(), ToStartActivity.class), 3333);
}
The trick is using an auxiliary temp fragment.
I'm having the same issue, as the initial question. I know that you all posted working solution, BUT, all the solutions lack one thing: encapsulation. What do I mean - If in one activity I have 10 views that should (on some event) start another activity it should be NORMAL to be able to start that new activity from the view that needs that activity. You all are trying to convince that is better to handle all new possible activites from the initial one - than why we added different logic in each view. We may want to RE-USE code, and create one custom view that can work INDEPENDENT to where we use it (work may include showing another activity to select something for example).
I know that this is not possible (or not yet), and is a clear proof that Android SDK is not ready yet to handle real big applications.
If you want an example:in any real business app that has for example, customer list (that should be a view) ,the view should be able to launch by itself addcustomer activity, edit customer activity and so on, independent from where you put that customer list view (control) - because in big apps you need to RE-use components (you may need to show the customer list control in a order product activity, in a timesheet activity and so on.).
One possible solution could be:
- start the new activity (using the view context (normally should be the parent activity).
- on the new activity closing event, either call directly a method in the calling view (depending on the case, and posibilities: either static that is handling the code that you normally would run on activityresult, either try to pass the instance of the calling view to the new activity, and do the same. In this way, you can handle your new activity, without letting the containing activity to know anything about it.
You need to catch this from your activity. The startActivityForResult is called on your activity, so it'll be the one launching the Intent and getting the result. I'd say that it's overall bad to launch it directly from the view's code. A better solution would be with a clickListener (or checkChangeListener, or whatever you want), set by your activity, and calling a method like "openImageCapture".
When the Intent returns, your activity will take care of the result and update your views as needed.
Views are there just for displaying stuff on the screen and getting user input, the activity is there to do the actual work.
Here's a static function to implementing #riwnodennyk's solution, while overcoming the Fragment must be static and not in anonymous class error:
public static void myStartActivityForResult(FragmentActivity act, Intent in, int requestCode, OnActivityResult cb) {
Fragment aux = new FragmentForResult(cb);
FragmentManager fm = act.getSupportFragmentManager();
fm.beginTransaction().add(aux, "FRAGMENT_TAG").commit();
fm.executePendingTransactions();
aux.startActivityForResult(in, requestCode);
}
public interface OnActivityResult {
void onActivityResult(int requestCode, int resultCode, Intent data);
}
#SuppressLint("ValidFragment")
public static class FragmentForResult extends Fragment {
private OnActivityResult cb;
public FragmentForResult(OnActivityResult cb) {
this.cb = cb;
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (cb != null)
cb.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
getActivity().getSupportFragmentManager().beginTransaction().remove(this).commit();
}
}
Usage example:
Intent inPhonebook = new Intent(Intent.ACTION_PICK, ContactsContract.CommonDataKinds.Phone.CONTENT_URI);
myStartActivityForResult((FragmentActivity) getContext(),
inPhonebook, REQUEST_CODE_PICK_CONTACT, this::onContacts);
There is no way to catch onActivityResult from your view, only from Activity.
And its not safe to assume that's Context object is Activity. In general you should not rely on this fact. Even if it seems reasonable in case with views, you still should use only methods available trough Context interface. That's because your can't predict all side-effects on the Activity, when you're calling Activity specific functions.
Just make the same method inside your custom view
And inside the activitys onActivityResult call yourView.onActivityResult(...) and process the result inside your view..
Also as guys mentioned you must not always end up with Context being of Activity class. Usually when it is from inflated view.
But if you construct your view only in code and always use the activity instance you are good.

Categories

Resources