ChatGPT解决这个技术问题 Extra ChatGPT

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?


L
LeffelMania

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.


Additionally when loading one fragment from another you can set the target fragment and call back to the parent fragment's onActivityResult method if you so wish.
Your answer is incomplete, fragments are using a life cycle like activities. So, we can not juste pass arguments through methods but we should use bundles to pass values. Even if the fragment set a value somewhere, we need to know when he ended. Should the previous fragments get the value when it starts/resumes ? This is an idea. But there is no proper way to store the value, the fragment could be called by multiple other fragments/activities.
well, it would make sense to call 'start fragment for result' since it would give much more flexibility -> why should fragment as a child know about its parent? wouldn't be much cleaner for the fragment to just do its work and return result regardless of which activity he lives in. Especially in the context of single activity apps.
What about potential memory leaks?
n
nagoya0

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();
}

I have not confirmed it, but maybe it works. so, you should take care about memory leaks caused by circular reference by abuse of setTargetFragment().
Best solution so far! Works great when having one Activity and a stack of fragments where trying to find the right fragment to notify would be a nightmare. Thanks
@userSeven7s this won't work for replace case but should work for hide case
@nagoya0 does it better if we use WeakReference for target fragment
While this works, setTargetFragment is currently deprecated. See setResultListener in stackoverflow.com/a/61881149/2914140 here.
B
Boonya Kitpitak

Recently, Google has just added a new ability to FragmentManager which made the FragmentManager be able to act as a central store for fragment results. We can pass the data back and forth between Fragments easily.

You can out the blog post that I've made https://oozou.com/blog/starting-a-fragment-for-results-in-android-46

Starting fragment.

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // Use the Kotlin extension in the fragment-ktx artifact
    setResultListener("requestKey") { key, bundle ->
        // We use a String here, but any type that can be put in a Bundle is supported
        val result = bundle.getString("bundleKey")
        // Do something with the result...
    }
}

A Fragment that we want the result back.

button.setOnClickListener {
    val result = "result"
    // Use the Kotlin extension in the fragment-ktx artifact
    setResult("requestKey", bundleOf("bundleKey" to result))
}

The snippet is taken from Google's official documents. https://developer.android.com/training/basics/fragments/pass-data-between#kotlin

At the date of this answer written, this feature is still in alpha state. You can try it out using this dependency.

androidx.fragment:fragment:1.3.0-alpha05

C
Community

We can simply share the same ViewModel between fragments

SharedViewModel

import android.arch.lifecycle.MutableLiveData
import android.arch.lifecycle.ViewModel

class SharedViewModel : ViewModel() {

    val stringData: MutableLiveData<String> by lazy {
        MutableLiveData<String>()
    }

}

FirstFragment

import android.arch.lifecycle.Observer
import android.os.Bundle
import android.arch.lifecycle.ViewModelProviders
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup

class FirstFragment : Fragment() {

    private lateinit var sharedViewModel: SharedViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activity?.run {
            sharedViewModel = ViewModelProviders.of(activity).get(SharedViewModel::class.java)
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_first, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        sharedViewModel.stringData.observe(this, Observer { dateString ->
            // get the changed String
        })

    }

}

SecondFragment

import android.arch.lifecycle.ViewModelProviders
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGrou

class SecondFragment : Fragment() {

    private lateinit var sharedViewModel: SharedViewModel

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activity?.run {
            sharedViewModel = ViewModelProviders.of(activity).get(SharedViewModel::class.java)
        }
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_first, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        changeString()
    }

    private fun changeString() {
        sharedViewModel.stringData.value = "Test"
    }

}

Hi Levon, I think above code will not share same view model instance. You are passing this(fragment) for ViewModelProviders.of(this).get(SharedViewModel::class.java). This will create two separate instances for fragments. You need to pass activity ViewModelProviders.of(activity).get(SharedViewModel::class.java)
@ShailendraPatil Nice catch, I will fix it now.
A
AlikElzin-kilaka

My 2 cents.

I switch beween fragments by swapping an old fragment with a new one using hide and show/add (existing/new). So this answer is for devs who use fragments like I do.

Then I use the onHiddenChanged method to know that the old fragment got switched to back from the new one. See code below.

Before leaving the new fragment, I set a result in a global parameter to be queried by the old fragment. This is a very naive solution.

@Override
public void onHiddenChanged(boolean hidden) {
    super.onHiddenChanged(hidden);
    if (hidden) return;
    Result result = Result.getAndReset();
    if (result == Result.Refresh) {
        refresh();
    }
}

public enum Result {
    Refresh;

    private static Result RESULT;

    public static void set(Result result) {
        if (RESULT == Refresh) {
            // Refresh already requested - no point in setting anything else;
            return;
        }
        RESULT = result;
    }

    public static Result getAndReset() {
        Result result = RESULT;
        RESULT = null;
        return result;
    }
}

What is getAndReset() method?
Isn't also onResume() called on the first fragment when the second is dismissed?
S
Summved Jain

In your fragment you can call getActivity(). This will give you access to the activity that created the fragment. From there you can call your customize method to set the values or to pass the values.


R
Ragunath Jawahar

There is an Android library - FlowR that allows you to start fragments for results.

Starting a fragment for result.

Flowr.open(RequestFragment.class)
    .displayFragmentForResults(getFragmentId(), REQUEST_CODE);

Handling results in the calling fragment.

@Override
protected void onFragmentResults(int requestCode, int resultCode, Bundle data) {
    super.onFragmentResults(requestCode, resultCode, data);

    if (requestCode == REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            demoTextView.setText("Result OK");
        } else {
            demoTextView.setText("Result CANCELED");
        }
    }
}

Setting the result in the Fragment.

Flowr.closeWithResults(getResultsResponse(resultCode, resultData));

K
Kirk_hehe

The easiest way to pass data back is by setArgument(). For example, you have fragment1 which calls fragment2 which calls fragment3, fragment1 -> framgnet2 -> fargment3

In fragment1

public void navigateToFragment2() {
    if (fragmentManager == null) return;

    Fragment2 fragment = Fragment2.newInstance();
    String tag = "Fragment 2 here";
    fragmentManager.beginTransaction()
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
            .add(R.id.flContent, fragment, tag)
            .addToBackStack(null)
            .commitAllowingStateLoss();
}

In fragment2 we call fragment3 as usual

private void navigateToFragment3() {
    if (fragmentManager == null) return;
    Fragment3 fragment = new Fragment3();
    fragmentManager.beginTransaction()
            .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
            .replace(R.id.flContent, fragment, tag)
            .addToBackStack(null)
            .commit();
}

When we finished our task in fragment3 now we call like this:

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
if (fragmentManager == null) return;
fragmentManager.popBackStack();
Bundle bundle = new Bundle();
bundle.putString("bundle_filter", "data");
fragmentManager.findFragmentByTag("Fragment 2 here").setArguments(bundle);

Now in fragment2 we can easily call arguments

@Override
public void onResume() {
    super.onResume();
    Bundle rgs = getArguments();
    if (args != null) 
        String data = rgs.getString("bundle_filter");
}

Be careful with this one, it could work in some cases, but if your fragment had original arguments ('fragment 2' in this example), this would override the original arguments used to create the fragment, which could lead to an inconsistent state if the fragment is destroyed and recreated.
J
Jake Lee

A solution using interfaces (and Kotlin). The core idea is to define a callback interface, implement it in your activity, then call it from your fragment.

First, create an interface ActionHandler:

interface ActionHandler {
    fun handleAction(actionCode: String, result: Int)
}

Next, call this from your child (in this case, your fragment):

companion object {
    const val FRAGMENT_A_CLOSED = "com.example.fragment_a_closed"
}

fun closeFragment() {
    try {
        (activity as ActionHandler).handleAction(FRAGMENT_A_CLOSED, 1234)
    } catch (e: ClassCastException) {
        Timber.e("Calling activity can't get callback!")
    }
    dismiss()
}

Finally, implement this in your parent to receive the callback (in this case, your Activity):

class MainActivity: ActionHandler { 
    override fun handleAction(actionCode: String, result: Int) {
        when {
            actionCode == FragmentA.FRAGMENT_A_CLOSED -> {
                doSomething(result)
            }
            actionCode == FragmentB.FRAGMENT_B_CLOSED -> {
                doSomethingElse(result)
            }
            actionCode == FragmentC.FRAGMENT_C_CLOSED -> {
                doAnotherThing(result)
            }
        }
    }

A
Arjun

Another thing you could do depending on your architecture is use a shared ViewModel between the fragments. So in my case FragmentA is a form, and FragmentB is a item selection view where the user can search and select an item, storing it in the ViewModel. Then when I come back to FragmentA, the information is already stored !