DialogFragment onActivity result never reached - android

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.

Related

Launching Fragment from activity

In my MainActivity, I have a viewPager
containing two containers "A and B".
Both containers are possessed with their own Fragments.
In container B; I again have a viewPager containing multiple fragments.(For example one,two,three,four)
On launch of my app MainActivity gets load with container A's homeFragment.
What I need to do is,For a given condition I have to launch fragment two of container B from onResume of my MainActivity.
What I've able to achieve is , I have successfully redirect to container B's fragment one but couldn't able to redirect towards fragment two from onResume of MainActivity, any help will be appreciate.
If you are using Intent to redirect to Fragment B, then pass the int position of sub fragment, and on the fragment B side check for intent extras and set the current page, which you received via intent.getIntExtra().
If I understood it clear, you want to show the second page of the viewpager when you open it, if so you can use mViewPager.setCurrentItem(1, true);
I think you want to set the specific page .. so to do that you can set viewpager as :
viewpager.setCurrentItem(item, true);
Certainly,I found a way of achieving this by using static concept.
I have declared a static variable inside container B's fragment two,and set its value in onResume of MainActivity.
When onResume calls with given condition it sets static variables value to the required fragment, and in my container B,I have used this value to set setCurrentItem() of B'viewPager.
ps: I know it quite confusing as it seems but not too complex in practice,work fine.

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

onResume like overide in a fragment

The Scene
I have an activity that houses 2 fragments .
Activity A is launched.
Fragment B completely takes over the activity A (UI wise).
Here depending on the operation , Fragment C launches and takes over the whole UI
The Target
I want to be able to know when a user presses back from Fragment C , and lands back on fragment B .
The Problem.
We all know that we CANNOT use the onResume override in the Fragment B , because this onResume is closely coupled with the Activity's lifecycle. If there was an override like onResume in Fragment B , it would have really helped my case. I know that the Fragment B is not onPaused when Fragment C overtakes it , so I am aware that onResume like strategy wont work .
The Solution
I think I can attach a callback onBackPressed in Activity A . Something like .
Psuedo Code
private onBackPressed()
{
if (currentFragment == Fragment C) {
FragmentB = findFragmentB();
//call custom method written in FragmentB
FragmentB.onBackPressed();
} else {
super.onBackPressed();
}
}
Before I proceeded , is this the right direction to move in ?
Exactly,,, if there is most suitable and easy solution for this problem. i think you should continue with this. I find out other ways but this was better and easy then others
Something alone the lines of this should work
BaseFragment currFrag = (BaseFragment) manager.findFragmentById(R.id.fragmentname);
currFrag.onFragmentResume();
Not much to explain here, onFragmentResume literally calls onResume of the 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!

Refreshing a fragment view from MainActivity

So I currently have an app that has 4 tabs (fragments). They are fragments A,B,C,D, in that order.
Fragment A is the first view opened (along with B because viewPager loads the view before and after the current view).
When I click a button in Fragment A, it sends Data back to MainActivity and then sends that data out to Fragments B and C.
However, this is where the issue comes into play. Since Fragment B was already called, the View isn't updated once I click the button and send the data over, but Fragment C is because the view wasn't called before.
Is there any way that I can remedy this?
You can do it a few ways right.
Just set the data to the fragment and have it update its views
Have all the fragments like B and C register themselves to recieve data from the MainActivity and when MainActivity gets it's data set you tell all the registered receivers of the new data
Recreate the fragment
Use an event bus and tell all subsribers of the new data and MainActivity, Fragment B would get notified of new data. Fragment C would get its data when created by MainActivity
I think this list is pretty endless tbh
The key here is the fragments need to fetch the data from the actvitiy aswell as be updated by the activity. In which case you need to break your UI update behaviour out of onCreateView and into its own updateUI() function. updateUI(MyData) can then be called from onCreateView and also called in a setMyData() on the fragment. Just make sure you check the isAdded flag in setMyData.
This pretty much says it all:
http://developer.android.com/training/basics/fragments/communicating.html
I used a simple fragment communicator that allows the activity to call the fragment, and the same for a fragment to talk to the activity.
You can change the views with the new data based on calling the method from within the activity. The way I do it is set the fragments in the activity then pass them into the page adapter this way I can call the methods within the fragment and implement the fragmentcommunicator interface on the fragments.
You can honestly even avoid the interface if you want, but if you are going to include the same method in all the fragments to talk to them it is easiest.
If you show code, I can show you a quick example.

Categories

Resources