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

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

问题描述

我有一个包含 3 个片段的活动 A.每个片段相互替换,因此在给定时间只有 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 代表一个来自 Fragment1 和 Fragment2 的文本值.当我点击说 Card1 时,我到达了 Fragment1.

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.

Fragment1 有一些卡片视图,当我选择它们中的任何一个时,我导航回 HomeFragment 并根据我在 Fragment1 中的选择更新卡片视图文本.这是 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 与 Fragment 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 数据是设置为默认值.这就是我在 Home Fragments 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 和片段 2 的片段事务中创建了一个新的 Homefragment,我怎样才能只保留 1 个 Home 片段实例.

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.

推荐答案

Google 推荐的另一种设计是使用主 Activity 和 2 个片段(在您的情况下是 Fragment1 和 Fragment2).我可以看到您将数据包传递给 HomeFragment 的问题.此建议的设计使用声明为静态的 MainActivity(可能需要范围问题).它使用接口在Activity和Fragment之间建立.我认为接口比将 bundle 传回 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.

Google 网页是@与其他片段通信.这不仅仅是我的意见.我认为,一个很好的 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 snippet from the webpage...

Fragment 到 Activity 通信的示例:

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);
   }
...

Activity 到 Fragment 通信的示例:

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 是 Fragment 创建的接口.
  • 创建的方法 onArticleSelected 有一个参数 position,它来自 ListFragment 中的 ListView(示例中).
  • 您仍然可以设置数据包并在 Activity 和 Fragment 之间发送数据.但是我还没有将数据从 Fragment 发送回 Activity.我通常使用 Fragment 来处理大部分 UI 更新.
  • 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天全站免登陆