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
Related
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
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!
I have been asked an interview question: Can a fragment exist without activity? I searched for answers but didn't get a proper answer and explanation. Can someone help?
Yes, you can do this anywhere:
new YourFragment();
As fragments must have a parameter-less constructor.
However its lifecycle doesn't kick in until it is attached. So onAttach, onCreate, onCreateView, etc. are only called when it is attached. So most fragments do nothing until they are attached.
It can exist as an object in memory (by creating it with new), but it needs to be attached to an Activity in order to appear on the screen, assuming it has any UI (fragments don't have to have UI).
A Fragment can exist independently, but in order to display it, you need the help of an Activity. The Activity will act like a container for the Fragment(s).
A fragment is not required to be a part of the Activity layout; you may also use a fragment without its own UI as an invisible worker for the Activity but it needs to be attached to an Activity in order to appear on the screen.
As soon as you create an instance of the Fragment class, it exists, but in order for it to appear on the UI, you must attach that fragment to an activity because a fragment's lifecycle runs parallel to an activity's lifecycle. Without any call to Activity's onCreate(), there will be no call for onAttach(), onCreate(), onCreateView() and onActivityCreated() of fragment and so it can't be started.
I read above top rated answer , i am not disagreeing but android already provides to make independent fragment without activity DialogFragment , which extends fragment . if you want show in full screen first extends DialogFragment then
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_FRAME, android.R.style.Theme_Holo_Light);
}
Android app must have an Activity or FragmentActivity that handles the fragment.
Fragment can't be initiated without Activity or FragmentActivity.
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 2 Tabs and 2 Corresponding Fragments. On calling the LAUNCH Activity both Tabs were added and then the first one added will be shown. Thus the first Fragments onCreateView is called the second Fragments ones not.
In my case this is an issue because the first Fragment has to call methods on the second Fragment. Inside the second Fragment there is an Objectreference which will be set by calling the onCreateView in the second Fragment.
Therefore I used following code snippet to solve this
actionBar.setSelectedNavigationItem(1);
actionBar.setSelectedNavigationItem(0);
It works but in my opinion there must be another possibility to solve this issue. Like calling the onCreateView of the second Fragment?
Here is the relevant code snippet. The listener is implemented as in android-dev Sample only with small changes not affecting my issue.
simplexFragment corresponds to the first Fragment
graphicFragment corresponds to the second Fragment
// adds two tabs
actionBar.addTab(actionBar.newTab().setText("Input").setTabListener(new TabListener(null, "input_fragment")));
graphicFragment = new GraphicFragment();
actionBar.addTab(actionBar.newTab().setText("Graphic").setTabListener(new TabListener(graphicFragment, "graphic_fragment")));
simplexFragment.setGraphics(graphicFragment); // sets the internal reference!
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// selects the Tab so the GraphicFragments onCreateView will be called
actionBar.setSelectedNavigationItem(1);
actionBar.setSelectedNavigationItem(0);
Thanks for support.
In my case this is an issue because the first Fragment has to call methods on the second Fragment.
This is not how Fragments are meant to work. The idea is a Fragment should be self-contained and re-usable and one Fragment shouldn't know that another exists (or at least shouldn't rely the existence any other Fragment).
For example, suppose you have 3 Fragments and ActivityA uses FragmentA and FragmentB but you have another Activity (ActivityB) which uses FragmentA and FragmentC. In that case, FragmentA doesn't know what the other Fragment is (B or C) and shouldn't even expect there to be another Fragment at all.
The way to achieve what you want is to use callback methods on the Activity and have it perform the actions on any other Fragments (if they exist). It can do this by either calling public methods on the other Fragments or by passing data in the 'arguments' Bundle when it creates the other Fragments.