【问题标题】:Passing data between two fragments in two different activities在两个不同活动中的两个片段之间传递数据
【发布时间】:2014-02-03 23:09:33
【问题描述】:

我正在尝试使用 Fragments 做一个非常基本的示例。

结构:Fragment1 -> Fragment1Activity,Fragment2 -> Fragment2Activity。

这两个活动都有一个静态片段。

这里是 XML:

activity_for_fragment_1.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.xx.fragmentstutorial1.Fragment1"
    tools:context="com.xx.fragmentstutorial1.Fragment1Activity"/>

fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview_fragment_1"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.60"
        android:text="This is Fragment 1"
        android:gravity="center"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/edittext_fragment_1_text"
        android:layout_width="match_parent"
        android:layout_height="0dip"
        android:layout_weight="0.20"
        android:text="Type your message here..." >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button_fragment_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Click Me" />

</LinearLayout>

activity_for_fragment_2.xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/fragment_2"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:name="com.xx.fragmentstutorial1.Fragment2"
    tools:context="com.xx.fragmentstutorial1.Fragment2Activity"/>

fragment_2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textview_fragment_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="This is Fragment 2"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textview_fragment_2_result"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="I will display text from \nFragment 1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

现在,我在 fragment_1 中有一个 Edittext 和按钮。当我点击按钮时,我想获取 EditText 中输入的文本并将其设置为 fragment_2 中的textview(textview_fragment_2_result)

我能够做到这一点,但是,我不太相信我采用的方法已经足够好。请看下面的java代码..

Fragment1Activity.java

public class Fragment1Activity extends SherlockFragmentActivity implements Fragment1.ButtonClickListener{

    Fragment1 fragment1;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_for_fragment_1);
        System.out.println("onCreate Fragment1Activity");
        fragment1 = (Fragment1) getSupportFragmentManager().findFragmentById(R.id.fragment_1);

    }

    @Override
    public void onButtonClick(String message) {
        System.out.println("onButtonClick Fragment1Activity");
        startActivity(new Intent(this, Fragment2Activity.class).putExtra("message", message));
    }

}

Fragment1.java

public class Fragment1 extends SherlockFragment {

    EditText message;
    Button clickme;

    ButtonClickListener listener;

    public interface ButtonClickListener{
        public void onButtonClick(String message);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (activity instanceof ButtonClickListener)
            listener = (ButtonClickListener) activity;
        else {

        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        System.out.println("onCreateView Fragment1");

        View view = inflater.inflate(R.layout.fragment_1, container, false);

        message = (EditText) view.findViewById(R.id.edittext_fragment_1_text);
        clickme = (Button) view.findViewById(R.id.button_fragment_1);
        clickme.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                if (!message.getText().toString().equalsIgnoreCase("")) {
                    System.out.println("Message in Fragment1 = "+message.getText().toString());

                    listener.onButtonClick(message.getText().toString());
                } else {
                    Toast.makeText(getActivity(),
                            "Please enter some message...", Toast.LENGTH_LONG)
                            .show();
                }

            }
        });

        return view;
    }

}

Fragment2Activity.java

public class Fragment2Activity extends SherlockFragmentActivity {

    Fragment2 fragment2;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_for_fragment_2);

        fragment2 = (Fragment2) getSupportFragmentManager().findFragmentById(R.id.fragment_2);
        fragment2.setMessage(getIntent().getExtras().getString("message").toString());
    }

}

Fragment2.java

public class Fragment2 extends SherlockFragment {

    String msg;
    TextView textview;

    public Fragment2() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_2, container, false);
        textview = (TextView) view
                .findViewById(R.id.textview_fragment_2_result);
        textview.setText(msg);
        return view;
    }

    public void setMessage(String message) {
        msg = message;
        textview.setText(msg);
    }
}

我在 Fragment2.java 的 setMessage() 中设置了 textview 的文本,我认为这不是一个好方法。如果我将其注释掉,我在 Fragment2 的文本视图中看不到任何文本。

有人可以帮助我,了解如何在两个静态片段之间正确传递值。

【问题讨论】:

  • 您所做的是人们使用的一种方法,或者将值存储在共享首选项中,并在开始活动后从第二个片段中获取它,就像您一样

标签: android android-fragments


【解决方案1】:

你应该选择interface

按照这里的图表:

两个 Fragment 之间的通信是通过 Activity 使用接口完成的

FragmentA------------------------->Activity------------- ------->FragmentB

(定义Interface)(实现interface)(传递给其他片段B)

希望对你有帮助

Check This Out

【讨论】:

  • 那不是我想要的.. FragmentA -> Activity A -> Activity B -> Fragment B,是我要找的..
【解决方案2】:

你所拥有的大多是好的。将数据从 Fragment1 传输到 Activity1 的部分是好的。一旦数据到达 Activity2,您将在 Fragment2 中设置它。我会稍微改变这部分。

在 Activity2 中:

@Override
protected void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setContentView(R.layout.activity_for_fragment_2);

    fragment2 = (Fragment2) getSupportFragmentManager().findFragmentById(R.id.fragment_2);
    fragment2.setArguments(getIntent().getExtras());
}

在 Fragment2 中。

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    Bundle bundle = getArguments();
    String message = bundle.getString("message");
    View view = inflater.inflate(R.layout.fragment_2, container, false);
    textview = (TextView) view
            .findViewById(R.id.textview_fragment_2_result);
    textview.setText(message);
    return view;
}

【讨论】:

    【解决方案3】:

    您的方法似乎是正确的。您的 Fragment1 和 Activity1 之间的连接就像在片段 guideline 中一样。您的 fragment1 不应该知道 fragment2,因此可以将责任传递给片段的容器/父级 - activity1 - 它知道如何处理数据。这一次你的方法又是正确的——activity1 启动它是从属/子等,并负责用正确的数据启动它(通过意图附加项传递)。然后你有一个正确的方法 - 当你的 activity2 创建时,你用适当的内容/数据(消息)填充它的视图,这些内容/数据是要在这个活动中显示并由“父”活动提供给它的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-30
      • 2018-04-16
      • 1970-01-01
      • 1970-01-01
      • 2018-09-25
      • 1970-01-01
      相关资源
      最近更新 更多