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

后端 未结 10 1281
孤城傲影
孤城傲影 2020-12-02 09:51

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

相关标签:
10条回答
  • 2020-12-02 10:12

    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.

    0 讨论(0)
  • 2020-12-02 10:15

    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 !

    0 讨论(0)
  • 2020-12-02 10:21

    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");
    }
    
    0 讨论(0)
  • 2020-12-02 10:23

    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();
    }
    
    0 讨论(0)
提交回复
热议问题