在两个不同的活动中传递两个片段之间的数据

时间:2014-01-27 09:08:08

标签: android android-fragments

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

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

两个活动都有一个STATIC片段。

以下是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的textview中看不到任何文字。

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

3 个答案:

答案 0 :(得分:0)

您应该使用interface

按照图表这里:

使用界面

通过活动完成两个片段之间的通信

Fragment A --------------------------&gt; Activity -------- ------------&gt; Fragment B

(定义{{​​1}})(实施Interface)(传递给其他片段B)

希望这可以提供帮助

Check This Out

答案 1 :(得分:0)

你所拥有的一切都是好的。将数据从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;
}

答案 2 :(得分:0)

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

相关问题