fragment's onActivityForResult is not response, specific as fllow - android

Has a MainActvity,the fragment is inside. The Fragment startActivityForResult to another Activity,and get data setResult() to the Fragment. The Mainctivity's launchMode is defult.
But,the Fragment's Method onActivityForResult() is not implement.

Make sure to call super.onActivityResult in your MainActivity if you override onActivityResult
That's the most common mistake but there are more cases which are covered here

Related

Check current fragment in viewpager that was attached in Activity's onAttachFragment()

I have a callback method I need to call once a fragment in my ViewPager is attached to my activity. In order to do this I'm overriding onAttachFragment(Fragment fragment) in the activity.
But I have 3 fragments in the ViewPager, so how can I check the fragment parameter in OnAttachFragment() to make sure it's the specific fragment I need?
If I am going about this wrong, I would like to know how to accomplish this the right way.
Figured it out! Instead of calling OnAttachFragment() in the activity, I'm calling the callback method in onActivityCreated() in the fragment where it needs to be called.

Nested Fragment in FragmentTabHost can not onActivityResult

I create an application as below:
Main activity includes FragmentTabHost which includes two ContainerFragments which include two Fragments separately.
Now I startActivityForResult from UserFragment, but onActivityResult in UserFragment cannot be called! What's wrong? How to resolve?
The project in https://github.com/VictorS-Zhao/TabHostAndNestedFragment.
Thanks in advance!
Supplement:
This is a strange behavior, the 1st nested fragment Community Fragment's onActivityResult can be called every time, but the 2nd nested fragment User Fragment's cannot be called. When startActivityForResult from UserFragment, but the onActivityResult in CommunityFragment is called.
Solution:
Finally I found solution in onActivityResult() not called in new nested fragment API
According to this answer, should override onActivityResult in ContainerFragment, and use getParentFragment().startActivityForResult in UserFragment. That's nice!
Provided that the UserFragment is your child fragment (a child of ContainerFragment), onActivityResult method does not execute once you return from your activity which has been started with
startActivityForResult.
It was a bug in the support library, I came over similar situation a while ago and it still was not fixed. Honestly, I am not sure whether it is fixed at the moment, you have to find out.
Here is a solution describing how to handle that:
http://inthecheesefactory.com/blog/how-to-fix-nested-fragment-onactivityresult-issue/en
https://gist.github.com/artem-zinnatullin/6916740
Basically the easiest way to make onActivityResult method executed in the child fragment is to start the activity from Activity level getActivity().startActivityForResult() or Parent Fragment getParentFragment().startActivityForResult() and notifying manually every fragment belonging to the calling Activity about invoked Activity's result.
Alternatively you can use some open source:
https://github.com/nuuneoi/StatedFragment
Hope that helps somehow.
Response to the Supplement:
Just as stated above, BaseContainerFragments belong to MainActivity, for those fragments onActivityResult method is called normally.
The problem begins when a child fragment is added to a parent fragment. For the child fragments onActivityResult is not called and you have to handle it manually in the Activity or the ParentFragment.
See :
onActivityResult() not called in new nested fragment API
onActivityResult is not being called in Fragment

Android nested Fragments onActivityResult issue

I have been reading very much about the nested Fragments onActivityResult issues.
I got the next conclusions.
1) At the Fragment, should call this.startActivityForResult() instead of this.getActivity.startActivityForResult()
2) Overwriting onActivityResult() at the parent Activity calling super.onActivityResult() to propagate the response through fragments.
Until here, the normal way to configure onActivityResult in Fragments.
But I use one implementation of nested fragments. Then I should do some more steps.
3) Here we can see the full process.
First, all my fragments are in root level, doesn't exist another fragment levels.
Then, to try solved the problem, I extend this fix Activity in the main Activity.
CommonActivity
Here, only one difference, I've replaced ActionBarActivity by FragmentActivity.
4)Finally, in the result ListActivity I have the next test code.
Intent output = new Intent();
output.putExtra("pos", position);
this.setResult(Activity.RESULT_OK, output);
this.finish();
THE QUESTION, debbuging, I can see when this.startActivityForResult() is called from Fragment, CommonActivity.startActivityFromFragment(..) is working. But, when response is throw from result ListActivity CommonActivity.onActivityResult(..) never is called.
Why?, Where can be the problem?
When called startActivityForResult, results always go to the same Activity from where was called or to Activity the Fragment was attached to(your case), so you can try Override onAttach (Activity activity) - function of Fragment, and see to which Activity was attached the Fragment.
Or other way to call startActivityForResult as public function of CommonActivity, so results always will go to CommonActivity.onActivityResult. Good Luck with that!

DialogFragment onActivity result never reached

I have the following arrangement: CatActivity -> FragA -> FragB; where, FragA launches FragB using fragB.show(getChildFragmentManager(), "FragB”);.
FragB launches the gallery intent with
Intent img = new Intent(Intent.ACTION_GET_CONTENT);
img.setType("image/*");
startActivityForResult(Intent.createChooser(img, “Choose Photo”),PHOTO_REQUEST_CODE);
The problem is that the FragB’s onActivityResult is never reached. Does anyone know why?
FYI: neither CatActivity nor FragA implements its respective onActivityResult method.
as far as my experience goes I've come to terms with the fact that onActivityResult is a complete mess.
For your particular situation I believe you should call startActivityForResult from fragA via getParent method in FragB. Then get the result in FragA and pass it onto the child fragment using getChildFragmentManager().getFragments().
I have followed this pattern for a FragmentTabHost where the individual tabs are child Fragments. There doesn't seem to be any default code to pass the activity result onto child fragments.

My callback called before onResume and cannot change layout

I really need your help here.
I have two Fragments:
1. Fragment A
2. Fragment B
One Interface
1. onSkillsSelectedListener ( method onDoneClicked )
Fragment A implements onSkillsSelectedListener and when "DONE" button is called in Fragment B, i call onDoneClicked() , and Fragment B is destroyed.
Now method onDoneClicked in Fragment A is called before even OnResume in Fragment A is called so i cannot make any changes in my Layout.
How to fix this issue?
The fragments onResume() or onPause() will be called only when the Activities onResume() or onPause() is called. They are tightly coupled to the Activity.
http://developer.android.com/guide/components/fragments.html#Lifecycle
You need to find another way to implement this.
I FIX IT
Just changed the way i called my Fragment B
from replace to add
So now Fragment A is not even Paused and callback is working!
Cheers!

Categories

Resources