包含在一个活动的片段之间传递数据 [英] Passing data between fragments contained in an activity

查看:122
本文介绍了包含在一个活动的片段之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个片段的活性的。每个片段替换彼此,因此在给定时间只有1是可见的。

I have an activity A with 3 fragments. Each fragments replaces each other, hence at a given time only 1 is visible.

HomeFragment有2 textviews包裹在里面2 cardviews。每个cardview重新presents它来自片段1和Fragment2文本值。当我点击说卡1,我得到的片段1。

HomeFragment has 2 textviews wrapped inside 2 cardviews. Each cardview represents a text value which comes from Fragment1 and Fragment2. When I click on say Card1,I get to the Fragment1.

片段1有一定的cardviews,当我将选择其中的任何我导航回到HomeFragment和更新基于我选择在Fragment1.Here的cardview文本switch语句,这取决于什么卡的用户选择我把在一个包和它传递给HomeFragment。

Fragment1 has some cardviews, when I selects any of them I navigate back to HomeFragment and update the cardview text based on my selection in Fragment1.Here is the switch statement, depending upon what card user selects I put that in a bundle and pass it to HomeFragment.

 switch (v.getId()) {
        case R.id.card_view0:

            Fragment1Bundle.putString("Test", "Testing");
            bundle.putBundle("Fragment1Bundle", Fragment1Bundle);
            fragmentTransaction.setCustomAnimations(R.anim.slideup, R.anim.slidedown, R.anim.slideup, R.anim.slidedown);
            fragmentTransaction.replace(R.id.content_frame, fragment);
            fragment.setArguments(bundle);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

            break;

Fragment2具有相同的行为片段1。

Fragment2 has same behavior as Fragment 1.

switch (v.getId()) {
        case R.id.card_view0:

            Fragment2Bundle.putString("Test2", "Tetsing");
            bundle.putBundle("Fragment2Bundle", Fragment2Bundle);
            fragmentTransaction.setCustomAnimations(R.anim.slideup, R.anim.slidedown, R.anim.slideup, R.anim.slidedown);
            fragmentTransaction.replace(R.id.content_frame, fragment);
            fragment.setArguments(bundle);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();

            break;

我的问题是,我用包传递片段之间的数据,我家的片段得到更新与片段1中的数据,但是当我去片段2,使选择回到首页片段后,我的片段1数据设置为默认值。这就是我在做家庭片段onCreateView()

My challenge is that I am using bundles to pass data between fragments, My home fragment gets updated with the data it from fragment1 but when I go to fragment 2 and after making the selection come back to Home fragment, my fragment1 data is set to default. This is what I am doing in Home Fragments onCreateView()

  try {
          bundle1 = getArguments().getBundle("Fragment1Bundle");
          bundle2 = getArguments().getBundle("Fragment2Bundle");


          tv.setText(bundle1.getString("Test") == null ? null : bundle1.getString("Test"));

            tv2.setText(bundle2.getString("Test2") == null ? nul : bundle2.getString("Test2"));

   } catch (NullPointerException e) {
        Log.d(TAG, e.printStackTrace());
    }

我知道我创造了我的片段事务都片段1和fragment2新Homefragment,我怎样才能让家左右的片段只是一个实例。

I know that I am creating a new Homefragment in my fragment transaction in both fragment1 and fragment2, How can I keep just 1 instance of Home fragment around.

推荐答案

建议谷歌的另一个设计是使用主活动和2片段(在你的情况片段1和Fragment2)。我可以看到你传递的数据包,以HomeFragment的问题。这表明设计采用 MainActivity 被声明为static(可能需要的范围界定问题)。它使用了接口将活动与片段之间建立。我觉得界面比通过捆绑回到HomeFragment容易。

Another design recommended by Google is to use the main Activity and 2 fragments (in your case Fragment1 and Fragment2). I can see your problem of passing data bundle to HomeFragment. This suggested design uses MainActivity which is declared static (may be required for scoping issue). And it uses an interface to be established between Activity and a Fragment. I think the interface is easier than passing bundle back to the HomeFragment.

一个谷歌的网页是@ 沟通与其它片段的。这不仅是我的看法。一个SO很好的链接,我想,是如何通过数据之间的片段的。

A Google webpage is @ Communicating with Other Fragments. This is not just my opinion. A good SO link, I think, is How to pass data between fragments.

从网页code片段...

Code snippet from the webpage...

片段到活动通信的例子:

An example of Fragment to Activity communication:

public class HeadlinesFragment extends ListFragment {
   OnHeadlineSelectedListener mCallback;

   // Container Activity must implement this interface
   public interface OnHeadlineSelectedListener {
       public void onArticleSelected(int position);
   }
...

活动的一个例子,以分片通讯:

An example of Activity to Fragment communication:

public static class MainActivity extends Activity
        implements HeadlinesFragment.OnHeadlineSelectedListener{
    ...

    public void onArticleSelected(int position) {
        // The user selected the headline of an article from the HeadlinesFragment
        // Do something here to display that article
    }
}

请注意:

  • OnHeadlineSelectedListener 是由片段中创建的接口。
  • 在创建的方法onArticleSelected有一个参数位置,这是从的ListView 在ListFragment(样本中)。
  • 您仍然可以设置数据包与活动和片段之间发送数据。不过,我没有从片段发回数据的活动。我通常使用片段来处理多用户界面的更新。
  • OnHeadlineSelectedListener is the interface created by the Fragment.
  • The created method onArticleSelected has a parameter position, which comes from the ListView in ListFragment (in the sample).
  • You can still set data bundles and send data between Activity and Fragment. However I have not sent back data from Fragment to Activity. I normally use Fragment to handle much of UI updates.

这篇关于包含在一个活动的片段之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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