Android-如何将数据从活动传递到片段? [英] Android - How to pass data from activity to fragment?
问题描述
我需要将数据从活动传递到片段.我知道我可以使用bundle,但是一旦我传递了数据,就不能不调用并再次创建片段而发送数据.
I need to pass data from activity to fragment. I know I can use bundle , but once I passed data,I can't send data without calling and creating fragment again.
在我的活动中,某些事情可能会更改,我需要在不重新创建片段的情况下将这些更改通知我的片段.
In my activity, some thing may be changed and I need to notify my fragment for these changes without recreating fragment.
推荐答案
在Activity
中创建一个interface
,然后将数据通过interface
传递到fragment
.在您的fragment
中实现该interface
以获取数据.
Create one interface
in your Activity
and pass your data via the interface
to the fragment
. Implement that interface
in your fragment
to get data.
例如
MainActivity.class
public class MainActivity extends AppCompatActivity {
DataFromActivityToFragment dataFromActivityToFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentA fr = new FragmentA();
FragmentManager fm = getFragmentManager();
dataFromActivityToFragment = (DataFromActivityToFragment) fr;
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr);
fragmentTransaction.commit();
final Handler handler = new Handler();
final Runnable r = new Runnable() {
public void run() {
dataFromActivityToFragment.sendData("Hi");
}
};
handler.postDelayed(r, 5000);
}
public interface DataFromActivityToFragment {
void sendData(String data);
}
}
FragmentA.class
public class FragmentA extends Fragment implements MainActivity.DataFromActivityToFragment {
TextView text;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.content_main, null);
text = (TextView) rootView.findViewById(R.id.fragment_text);
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void sendData(String data) {
if(data != null)
text.setText(data);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
content_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/fragment_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
在上面的示例中,我采用Runnable
只是为了在创建fragment
之后延迟5秒发送数据.
In above example I have taken Runnable
just to send data with delay of 5 seconds after creation of fragment
.
这篇关于Android-如何将数据从活动传递到片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!