碎片交易和将价值传递给碎片的问题? [英] Issue with fragment transaction and passing values to fragment?

查看:43
本文介绍了碎片交易和将价值传递给碎片的问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

im在使用FragmentTransaction时遇到麻烦,并将值从activityA传递到fragmentA.我认为我遇到的问题是我制作了2个 new FragmentA()实例,由于创建了2个片段,所以这打破了 bundle.arguments(),并且我使用了 if(bundle!= null)来检查bundle是否为空.在这个问题上已经存在了一段时间了.

im having some troubles with FragmentTransaction and passing values from activityA to fragmentA. The problem i think i am having is that i make 2 new FragmentA()instances and this breaks bundle.arguments() because of the 2 created fragments and i use if(bundle != null)in my fragment to check if the bundle is null. Been at this problem for quite some time.

谢谢您的帮助.

这是我到目前为止所拥有的

This is what i have so far

在这种方法中,我将Viewpager设置为3个片段(因为我使用tabbedlayout)

In this method i setup my viewpager with 3 fragments(because im using a tabbedlayout)

 private void setupViewPager() {
    PagerAdapter fragmentAdapter = new PagerAdapter(getSupportFragmentManager());
    fragmentAdapter.addFragment(getString(R.string.fragment_a), new TodoFragment());
    fragmentAdapter.addFragment(getString(R.string.fragment_b), new DoingFragment());
    fragmentAdapter.addFragment(getString(R.string.fragment_c), new KoncanoFragment());
    ViewPager mViewPager = findViewById(R.id.container);
    mViewPager.setAdapter(fragmentAdapter);
    TabLayout mTabLayout = findViewById(R.id.tabs);
    mTabLayout.setupWithViewPager(mViewPager);
    mViewPager.setOffscreenPageLimit(fragmentAdapter.getCount() - 1);
    mViewPager.setCurrentItem(0);
}

此方法用于将分发包发送到我的FragmentA

And this method is used to send a bundle to my FragmentA

private void sendToFragToDo(Fragment f, Bundle value) {
    final android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    f.setArguments(value);
    fragmentTransaction.replace(R.id.container, f, "a");
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();

}

推荐答案

可以使用另一种方法将数据从活动传递到捆绑.在fragment内创建一个方法newInstance(您想作为参数传递的对象),如果您在Fragment类内键入AndroidStudio newInstance,它将自动为您建议实现,否则,它将看起来像这样:

There is a different approach to use to pass data from activity to bundle. Make a method newInstance(Objects you wanna pass as parameters) inside fragment, if you type in AndroidStudio newInstance inside Fragment class it will automatically suggest the implementation for you, if not it will look like this:

public static TodoFragment newInstance(data to pass){
 Bundle args = new Bundle();
 //then you put into args the data you wann pass with args specific key,value method
 fragment.setArguments(args);
 return fragment;
}

在活动中而不是新的TodoFragment()中,使用TodoFragment.newInstance(要传递的数据);要检索传递的数据,请在Fragment的onCreate()中使用getArguments()

In the activity instead of new TodoFragment(), you use TodoFragment.newInstance(data to pass); To retrieve the passed data you use getArguments(), in Fragment's onCreate()

这篇关于碎片交易和将价值传递给碎片的问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆