简体   繁体   English

如何使用底部导航将数据从活动发送到片段

[英]how to send data from activity to fragment with bottom navigation

Hi everyone can help me pls大家好,请帮助我

I want send data from activity to fragment but I using bottom navigation我想将数据从活动发送到片段,但我使用底部导航

I using Intent to send data from activity 1 to activity 2 (activity 2 have bottom navigation)我使用 Intent 将数据从活动 1 发送到活动 2(活动 2 有底部导航)

I want to send data to Home_Fragment what should I Used?我想将数据发送到 Home_Fragment 我应该使用什么?

  BottomNavigationView bottomNav =   findViewById(R.id.top_navigation);
    bottomNav.setOnNavigationItemSelectedListener(navListener);
    getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
            new Home_Fragment()).commit();
}
private BottomNavigationView.OnNavigationItemSelectedListener navListener =
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {

                Fragment selectedItem = null;
                switch (menuItem.getItemId()){

                    case R.id.navigation_home:
                        selectedItem = new Home_Fragment();
                        break;

                    case R.id.navigation_project:
                        selectedItem = new Project_Fragment();
                        break;

                    case R.id.navigation_persons:
                        selectedItem = new Persons_Fragment();
                        break;

                    case R.id.navigation_accounts:
                        selectedItem = new Accounts_Fragment();
                        break;

                    case R.id.navigation_other:
                        selectedItem = new Others_Fragment();
                        break;
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
                        selectedItem).commit();
                return true;

            }
        };

just initialize your fragment from itself and pass any data inside initialize method.只需从自身初始化您的片段并在初始化方法中传递任何数据。

so by example if we want to pass a String value to fragmen we should make it like this inside fragment:因此,例如,如果我们想将 String 值传递给 Fragmen,我们应该在 Fragment 内部进行如下设置:

public static YourFrament getInstance(String example) {
        YourFrament fragment = new YourFrament();
        Bundle bundle = new Bundle();
        bundle.putString("key", example);
        fragment.setArguments(bundle);
        return fragment;
    }

and to get data you should receive it from onCreate method inside fragment like this:要获取数据,您应该从片段内的 onCreate 方法接收数据,如下所示:

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null)
            String value = getArguments().getString("key");
    }

so from activity we should call fragment like this:所以从活动中我们应该像这样调用片段:

case R.id.navigation_accounts:
             selectedItem = YourFrament.getInstance("string example");
          break;

Assuming you want to pass the data when you initialize the fragment , you could try and create a Bundle object, add your data to the bundle.假设您想在初始化fragment时传递数据,您可以尝试创建一个Bundle object,将您的数据添加到捆绑包中。

Then initialize your fragments using a static newInstance(Bundle args) passing in your bundle.然后使用在您的包中传递的 static newInstance(Bundle args)初始化您的片段。

So basically your fragments would look something like this.所以基本上你的片段看起来像这样。

public class HomeFragment extends Fragment{

public static Fragment newInstance(Bundle args){ 
// get your data and do whatever
return new HomeFragment(); }

Then in your onNavigationItemSelected() method然后在你的onNavigationItemSelected()方法中

 case R.id.navigation_home:

 Bundle bundle = new Bundle();
 bundle.putInt(AGE, 22); // put whatever data you want to pass to the fragment.

 selectedItem = HomeFragment.newInstance(bundle)
 break;

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

相关问题 Kotlin:使用 Kotlin 提供的标准底部导航活动时如何将数据从 MainActivity 传递到片段 - Kotlin: How to pass data from the MainActivity to a fragment when working with the standard bottom navigation activity provided by Kotlin 从片段打开活动后如何保持底部导航视图? - How to keep Bottom Navigation View after opening an Activity from Fragment? 如何从片段更改主要活动底部导航中的徽章编号? - How to change badge number in bottom navigation of main activity from fragment? 如何从活动返回导航底部的特定片段? - How to return from activity to a specific fragment in navigation bottom? 如何从活动返回到导航底部的特定片段 - How to return from activity to a specific fragment in navigation bottom 如何将(日期)等数据从活动发送到底部工作表对话框片段? - how to send data like (Date) from activity to bottom sheet dialog fragment? 如何在相同的片段活动中将数据从片段发送到片段? - How to send data from fragment to fragment within same fragment activity? 如何使用底部导航活动更改片段? - How to change fragment with the Bottom Navigation Activity? 如何将数据从主要活动发送到片段 - How to send data from main activity to fragment 如何将数据从片段发送到另一个活动? - How to send data from fragment to another activity?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM