简体   繁体   English

如何使用androidx从另一个片段调用片段方法

[英]How to call fragment method from another fragment using androidx

I am using androidx jetpack navigation component in my application and I am confused on how to call a method in previous fragment from the current fragment.我在我的应用程序中使用 androidx jetpack 导航组件,我对如何从当前片段调用前一个片段中的方法感到困惑。

There is currently no resource online on how to do this.目前没有关于如何执行此操作的在线资源。

My implementation is below...我的实现如下...

FRAGMENT A片段A

This is the code sample of the first fragment.这是第一个片段的代码示例。

public class FragmentOne extends Fragment{

    private String holder;

    private NavController navController;
    private Button btn_next;

    private void findViews(View view){
        btn_next = view.findViewById(R.id.btn_next);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_one, container, false);

    return view;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        navController = Navigation.findNavController(view);
        findViews(view);

        holder = "VALUE IN HOLDER VARIABLE";

        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                navController.navigate(R.id.action_fundone_to_fragmenttwo);
            }
        });


    }

    public void executeInFragmentTwo(){
        System.out.println(holder);
        Toast.makeText(getContext(), holder, Toast.LENGTH_LONG).show();
    }

}

FRAGMENT TWO片段二

This is the code sample of the second fragment这是第二个片段的代码示例

public class FragmentTwo extends Fragment{

    private NavController navController;
    private Button btn_exec_fragment_one_method;

    private void findViews(View view){
        btn_exec_fragment_one_method= view.findViewById(R.id.btn_exec_fragment_one_method);
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_one, container, false);

    return view;

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        navController = Navigation.findNavController(view);
        findViews(view);

        btn_exec_fragment_one_method.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                //HOW DO I CALL FRAGMENT ONE METHOD FROM HERE

                //The method with name "executeInFragmentTwo"

            }
        });
    }

}

Please any help on this would really be appreciated.请对此提供任何帮助,我们将不胜感激。

Your first fragment will not be in a resumed state when the user is onto the second fragment.当用户进入第二个片段时,您的第一个片段不会处于恢复状态。

If you need a function to be executed from another screen/fragment then you probably need to move this function to a ViewModel that is either tied to the lifecycle of the graph that these screens/fragments are part of or to the host activity lifecycle so that you get the same instance in both screens/fragments.如果您需要从另一个屏幕/片段执行一个函数,那么您可能需要将此函数移动到与这些屏幕/片段所属的图形的生命周期或主机活动生命周期相关联的ViewModel ,以便你在两个屏幕/片段中得到相同的实例。

To inject a model that lives as long as your graph you use the following syntax:要注入与您的图形一样长的模型,请使用以下语法:

private val model: MyViewModel by navGraphViewModels(R.id.myGraph) { ViewModelFactory() }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM