讲到数据传递,这里得先了解Fragment的生命周期:
Activity传递数据给Fragment:
在Activity中创建Bundle数据包,调用Fragment实例的setArguments(bundle)从而将Bundle数据包传给Fragment,然后Fragment中调用getArguments获得Bundle对象,然后进行解析就可以了。
下面来看几个例子:
一、利用Bundle生成初始值
1.存值:
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.fragment_static.MainActivity"
android:orientation="vertical"
>
<EditText
android:hint="input"
android:id="@+id/main_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/send_btn"
android:text="传递数据"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_weight="1"
android:id="@+id/fragment_content"
android:layout_width="wrap_content"
android:layout_height="0dp"></FrameLayout>
</LinearLayout>
MainActivity:
package com.example.administrator.fragment_massage1;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener{
private Button send_btn;
private EditText inputEt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send_btn=(Button)findViewById(R.id.send_btn);
send_btn.setOnClickListener(this);
inputEt=(EditText)super.findViewById(R.id.main_et);
AFragment aFragment=new AFragment();
String s="这是初始值";
Bundle bundle=new Bundle();
bundle.putString("data",s);
aFragment.setArguments(bundle);
FragmentManager fragmentManager=getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_content,aFragment).commit();
}
public void onClick(View view){
}
}
2.取值
fragment_a.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fragment_tv"
android:text="这是A"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
package com.example.administrator.fragment_massage1;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by Administrator on 2017/3/21.
*/
public class AFragment extends Fragment {
private TextView textView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a,container,false);
}
@Override
public void onViewCreated(View view,Bundle savedInstanceState){
super.onViewCreated(view,savedInstanceState);
//当Fragment的视图创建完成之后会回调
textView=(TextView)view.findViewById(R.id.fragment_tv);
}
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
//做数据的处理
Bundle bundle=getArguments();
String s=bundle.getString("data");
textView.setText(s);
}
}
二、上面的方法只适合用于Fragment拿到一些初始值,因为如果调用了setArguments,那么Fragment必须调用getArguments才能拿到Bundle。但是如果Fragment的生命周期已经显示到了屏幕上,它是没办法走回onActivityCreated这个周期的。所以如果Fragment已经显示到了屏幕上,可以在Fragment类中自定义一个set方法,然后在Activity中调用,因为这个Fragment的对象是在Activity中new出来的,自然可以调用自身的方法了。下面自定义一个setData方法来实现这样的功能:
布局文件不变:
AFragment:
package com.example.administrator.fragment_massage1;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* Created by Administrator on 2017/3/21.
*/
public class AFragment extends Fragment {
private TextView textView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a,container,false);
}
@Override
public void onViewCreated(View view,Bundle savedInstanceState){
super.onViewCreated(view,savedInstanceState);
//当Fragment的视图创建完成之后会回调
textView=(TextView)view.findViewById(R.id.fragment_tv);
}
public void setDate(String s){
if(textView!=null){
textView.setText(s);
}
}
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
//做数据的处理
Bundle bundle=getArguments();
String s=bundle.getString("data");
textView.setText(s);
}
}
MainActivity:
package com.example.administrator.fragment_massage1;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener{
private Button send_btn;
private EditText inputEt;
private AFragment aFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send_btn=(Button)findViewById(R.id.send_btn);
send_btn.setOnClickListener(this);
inputEt=(EditText)super.findViewById(R.id.main_et);
aFragment=new AFragment();
String s="这是初始值";
Bundle bundle=new Bundle();
bundle.putString("data",s);
aFragment.setArguments(bundle);
FragmentManager fragmentManager=getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_content,aFragment).commit();
}
public void onClick(View view){
String s=inputEt.getText().toString();
aFragment.setDate(s);
}
}
Fragment传递数据给Activity:
因为Fragment中没有Activity的对象,所以可以在Fragment中定义一个内部回调接口,再让包含该Fragment的Activity实现该回调接口,Fragment就可以通过回调接口传递数据了。
fragment_a.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fragment_tv"
android:text="这是A"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:text="向Activity中添加数据"
android:id="@+id/fragment_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
AFragment:
package com.example.administrator.fragment_massage1;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Administrator on 2017/3/21.
*/
public class AFragment extends Fragment {
private TextView textView;
private Button button;
private DataChange dataChange;
public void setDataChange(DataChange dataChange){
this.dataChange=dataChange;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_a,container,false);
}
@Override
public void onViewCreated(View view,Bundle savedInstanceState){
super.onViewCreated(view,savedInstanceState);
//当Fragment的视图创建完成之后会回调
textView=(TextView)view.findViewById(R.id.fragment_tv);
button=(Button)view.findViewById(R.id.fragment_btn);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
if(dataChange!=null){
String s=textView.getText().toString();
dataChange.onDataChange(s);
}
}
});
}
public void setDate(String s){
if(textView!=null){
textView.setText(s);
}
}
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
//做数据的处理
Bundle bundle=getArguments();
String s=bundle.getString("data");
textView.setText(s);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.administrator.fragment_static.MainActivity"
android:orientation="vertical"
>
<EditText
android:hint="input"
android:id="@+id/main_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="@+id/send_btn"
android:text="传递数据"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/main_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:layout_weight="1"
android:id="@+id/fragment_content"
android:layout_width="wrap_content"
android:layout_height="0dp"></FrameLayout>
</LinearLayout>
MainActivity:
package com.example.administrator.fragment_massage1;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity implements View.OnClickListener{
private Button send_btn;
private EditText inputEt;
private AFragment aFragment;
private TextView mainTv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
send_btn=(Button)findViewById(R.id.send_btn);
mainTv=(TextView)super.findViewById(R.id.main_tv);
send_btn.setOnClickListener(this);
inputEt=(EditText)super.findViewById(R.id.main_et);
aFragment=new AFragment();
String s="这是初始值";
Bundle bundle=new Bundle();
bundle.putString("data",s);
aFragment.setArguments(bundle);
aFragment.setDataChange(new DataChange() {
@Override
public void onDataChange(String data) {
mainTv.setText(data);
}
});
FragmentManager fragmentManager=getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment_content,aFragment).commit();
}
public void onClick(View view){
String s=inputEt.getText().toString();
aFragment.setDate(s);
}
}
DataChange接口:
package com.example.administrator.fragment_massage1;
/**
* Created by Administrator on 2017/3/21.
*/
public interface DataChange {
void onDataChange(String data);
}