简体   繁体   English

使用导航架构组件启动带有结果的片段

[英]launch a fragment with result using navigation architecture components

Below is how i am navigating from fragment A to fragment B using android navigation下面是我如何使用 android navigatingfragment A navigationfragment B


    @Override
    public void onClick(View view) {

        int viewId = view.getId();

        if (viewId == R.id.btn_proceed) {
          
          Navigation.findNavController(view).navigate(R.id.fragmentB);

        }
    }

I would like to achieve returning of results from fragmentB to fragmentA where for example fragmentB has an EditText that a user keys in text and is returned to fragmentA and set to TextView using android architecture navigation components我想实现从fragmentBfragmentA的结果返回,例如 fragmentB 有一个EditText ,用户输入文本并返回到 fragmentA 并使用 android 架构导航组件设置为TextView

According to documentation on returning a result i was able to achieve what i was asking with the code below.根据返回结果的文档,我能够使用下面的代码实现我所要求的。

Declare a NavController声明一个NavController

private NavController navController;

Inside onViewCreated method onViewCreated方法内部


navController = Navigation.findNavController(view);

        MutableLiveData<String> liveData = Objects.requireNonNull(navController.getCurrentBackStackEntry())
                .getSavedStateHandle()
                .getLiveData("key");

To get result in FragmentA from FragmentBFragmentB得到FragmentA的结果


 liveData.observe(getViewLifecycleOwner(), s -> {

           tvResult.setText(s);

        });

In FragmentB do the following, for example when a button is clickedFragmentB中执行以下操作,例如单击按钮时

@Override
    public void onClick(View view) {

        int viewId = view.getId();

        NavController navController = Navigation.findNavController(view);

        if (viewId == R.id.btn_returnResult) {

            String etText = editText.getText().toString();

            Objects.requireNonNull(navController.getPreviousBackStackEntry())
                    .getSavedStateHandle().set("key", etText);

            navController.navigateUp();

        }
    }

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

相关问题 如何从导航抽屉的片段中启动活动 - how to launch an activity from a fragment of a navigation drawer 如何在旧片段上滑动时为 Android 导航架构片段设置动画? - How to animate Android Navigation Architecture fragment as sliding over old fragment? 当点击从 WorkManager 发送的通知时,使用导航组件启动特定片段 - Launch a particular Fragment using Navigation component when tapped on Notification that is sent from WorkManager 使用导航组件单击 RecyclerView 项目时如何从 RecyclerView OnClick 侦听器更改片段 - How to change fragment from the Recycler View OnClick Listener when RecyclerView item is clicked using Navigation Components 使用导航组件折叠工具栏 - Collapsing toolbar using navigation components 将值从适配器传递到导航组件中的底部导航视图片段 - Passing value from Adapter to a BottomNavigationView Fragment in Navigation components 导航菜单片段无法初始化其布局的组件 - Navigation Menu Fragment not being able to initialize its layout's components 如何使用导航抽屉onClickListener()打开片段 - How open fragment using navigation drawer onClickListener() 无法使用导航组件导航到所需的片段 - Unable to navigate to desired fragment using navigation component 使用导航切换到另一个片段时保存片段数据 - Save Fragment data when switch to another fragment using Navigation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM