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.
Related
I am looking for a callback for fragment transaction on an Activity.
So, I have an activity in that i keep on changing the fragment, i need a callback every time a fragment is added or removed from the Activity. is there any callback for that or need to create my own callback??
Thanks
It looks like your only available callback is OnBackStackChangedListener.
Here i need to refresh the fragment from itself. So that i am going to using below code.
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.detach(this).attach(this).commit();
Above code is should be used in onstart() method or Oncreate() method for refreshing the current fragment from itself
From here you can under stand which method will call 1st and which after that,
So u can add this code where ever you want.
It should be onStart() You use the onCreateView() method in the fragment itself to define the xml layout that code will be using and the elements instantiated in that xml file.
On a more technical background, a fragment is always created in the onStart() method, hence taking precendence over the onCreateView() method. The reason is because in some cases, before displaying the fragment, the app needs to perform some background processes beforehand such as validation or getting data from the activity calling the fragment. This is done in the onStart() method before displaying the fragment.
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
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!
I have a ViewPager of fragments.
For each fragments, in the onCreateView(), I use AsyncTask to query the server.
I create the fragments in FragmentPagerAdapter's getItem().
However, because getItem() method will be called for the visible fragment and also the adjacent fragments, I am calling the onCreateView() and thus the AsyncTask of the adjacent Fragment even though the fragment is not visible.
How do I only call the AsyncTask of the visible fragment?
Is using onPageSelected() for the ViewPager the correct solution?
I used this method to call AsyncTask instead of in onCreateView() but then the onCreateView() of the fragment is not called.
Thank you.
You could override the method of Fragment:
setUserVisibleHint(boolean isVisibleToUser) {
super.setUserVisibleHint( isVisibleToUser);
if(isVisiableToUser) {
}
}