Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a method that works like start fragment for result?

I currently have a fragment in an overlay. This is for signing in to the service. In the phone app, each of the steps I want to show in the overlay are their own screens and activities. There are 3 parts of the sign-in process and each had their own activity that was called with startActivityForResult().

Now I want to do the same thing using fragments and an overlay. The overlay will show a fragment corresponding to each activity. The problem is that these fragments are hosted in an activity in the Honeycomb API. I can get the first fragment working, but then I need to startActivityForResult(), which isn't possible. Is there something along the lines of startFragmentForResult() where I can kick off a new fragment and when it's done have it return a result to the previous fragment?

like image 259
CACuzcatlan Avatar asked Jul 19 '11 17:07

CACuzcatlan


People also ask

Can you start an activity from a fragment?

If you want to start a new instance of mFragmentFavorite , you can do so via an Intent . Intent intent = new Intent(this, mFragmentFavorite. class); startActivity(intent); If you want to start aFavorite instead of mFragmentFavorite then you only need to change out their names in the created Intent .

How do you pass data via fragments?

To pass a result from a child fragment to a parent, the parent fragment should use getChildFragmentManager() instead of getParentFragmentManager() when calling setFragmentResultListener() . Figure 2 A child fragment can use FragmentManager to send a result to its parent.

Can we use fragment instead of activity?

It will not be wrong if we say a fragment is a kind of subactivity. Following are important points about a fragment: A fragment has its own layout and its own behavior with its own lifecycle callbacks. You can add or remove fragments in an activity while the activity is running.


2 Answers

All of the Fragments live inside Activities. Starting a Fragment for a result doesn't make much sense, because the Activity that houses it always has access to it, and vice versa. If the Fragment needs to pass on a result, it can access its Activity and set its result and finish it. In the case of swapping Fragments in a single Activity, well the Activity is still accessible by both Fragments, and all your message passing can simply go through the Activity.

Just remember that you always have communication between a Fragment and its Activity. Starting for and finishing with a result is the mechanism for communication between Activities - The Activities can then delegate any necessary information to their Fragments.

like image 167
LeffelMania Avatar answered Oct 24 '22 16:10

LeffelMania


If you wish, there are some methods for communication between Fragments,

setTargetFragment(Fragment fragment, int requestCode) getTargetFragment() getTargetRequestCode() 

You can callback using these.

Fragment invoker = getTargetFragment(); if(invoker != null) {     invoker.callPublicMethod(); } 
like image 36
nagoya0 Avatar answered Oct 24 '22 16:10

nagoya0