Android 如何将数据从一个片段传输到另一个片段
我知道的一种方法是通过活动。我们可以将数据从一个片段发送到另一个活动,还有其他方法吗 从文档中引用 通常,您希望一个片段与另一个片段通信,例如根据用户事件更改内容所有片段到片段的通信都是通过相关活动完成的。两个片段不应直接通信。 我建议您遵循文档中的方法,我没有尝试过任何其他替代方法 有关更多信息和示例,请查看下面链接中的文档Android 如何将数据从一个片段传输到另一个片段,android,fragment,Android,Fragment,我知道的一种方法是通过活动。我们可以将数据从一个片段发送到另一个活动,还有其他方法吗 从文档中引用 通常,您希望一个片段与另一个片段通信,例如根据用户事件更改内容所有片段到片段的通信都是通过相关活动完成的。两个片段不应直接通信。 我建议您遵循文档中的方法,我没有尝试过任何其他替代方法 有关更多信息和示例,请查看下面链接中的文档 将数据从一个片段传递到另一个包将有所帮助 LifeShapeDetailsFragment fragment = new LifeShapeDetailsFragment
将数据从一个片段传递到另一个
包将有所帮助
LifeShapeDetailsFragment fragment = new LifeShapeDetailsFragment(); // object of next fragment
Bundle bundle = new Bundle();
bundle.putInt("position", id);
fragment.setArguments(bundle);
然后推送/调用下一个片段。
和下一个片段的代码:
Bundle bundle = this.getArguments();
int myInt = bundle.getInt("position", 0);
在使用片段时,允许片段通过使用活动作为中介相互通信是一种常见的最佳实践。有关此重要模式的更多详细信息,请访问。无论何时需要与另一个片段交互,都应该在片段的活动中使用方法,而不是
直接访问另一个片段。从另一个片段访问一个片段唯一有意义的时候是当您知道不需要在另一个活动中重用您的片段时。你应该总是编写片段,假设你会重用它们而不是硬编码它们。 < p>有两种方法我认为可行:
A。与您拥有的活动进行通信,并通过该拥有的活动将消息转发给其他片段,有关详细信息可在以下android官方文档中找到:
引用:
在某些情况下,您可能需要一个片段来与共享事件
活动。一个很好的方法是定义一个回调接口
并要求宿主活动实现它。
当活动通过接口接收回调时,它可以
根据需要与布局中的其他片段共享信息
通信接口可以是这样的:
public interface IActionListener{
//You can also add parameters to pass along etc
public void doSomething();
}
public class MyFragment extends Fragment{
private WeakReference<IActionListener> actionCallback;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
actionCallback = new WeakReference<IActionListener>((IActionListener) activity);
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement IActionListener.");
}
}
}
public class MyActivity extends ActionBarActivity implements IActionListener {
public void doSomething(){ //Here you can forward information to other fragments }
}
片段看起来像这样:
public interface IActionListener{
//You can also add parameters to pass along etc
public void doSomething();
}
public class MyFragment extends Fragment{
private WeakReference<IActionListener> actionCallback;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
// This makes sure that the container activity has implemented
// the callback interface. If not, it throws an exception
actionCallback = new WeakReference<IActionListener>((IActionListener) activity);
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString() + " must implement IActionListener.");
}
}
}
public class MyActivity extends ActionBarActivity implements IActionListener {
public void doSomething(){ //Here you can forward information to other fragments }
}
B。至于第二种方法,您也可以使用接口让片段直接相互通信,这样您就不必知道您正在谈论的片段的确切类别,这确保了松散耦合
设置如下:您有两个片段(或更多)和一个活动(启动第二个片段)。我们有一个接口,让片段2在完成任务后向片段1发送响应。为了简单起见,我们只需重复使用我在A中定义的接口。
这是我们的片段1:
public class FragmentOne extends Fragment implements IActionListener {
public void doSomething() {//The response from Fragment 2 will be processed here}
}
使用A.Fragment 1中描述的方法要求它所属的活动启动Fragment 2。但是,活动将把片段1作为参数传递给片段2,因此片段2可以稍后间接访问片段1并发送回复。让我们看看活动是如何准备片段2的:
public class MyActivity extends ActionBarActivity {
// The number is pretty random, we just need a request code to identify our request later
public final int REQUEST_CODE = 10;
//We use this to identify a fragment by tag
public final String FRAGMENT_TAG = "MyFragmentTag";
@Override
public void onStartFragmentTwo() {
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
// The requesting fragment (you must have originally added Fragment 1 using
//this Tag !)
Fragment requester = manager.findFragmentByTag(FRAGMENT_TAG);
// Prepare the target fragment
Fragment target = new FragmentTwo();
//Here we set the Fragment 1 as the target fragment of Fragment 2's
//communication endeavors
target.getSelf().setTargetFragment(requester, REQUEST_CODE);
// Hide the requesting fragment, so we can go fullscreen on the target
transaction.hide(requester);
transaction.add(R.id.fragment_container, target.getSelf(), FRAGMENT_TAG);
transaction.addToBackStack(null);
transaction.commit();
}
}
提示:我使用的是支持框架,因此如果您只为Android 3.0开发,您可以简单地使用FragmentActivity而不是ActionBarActivity
现在Fragment2正在启动,让我们看看Fragment2将如何与FragmentOne通信:
public class FragmentTwo extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(savedInstanceState != null){
// Restore our target fragment that we previously saved in onSaveInstanceState
setTargetFragment(getActivity().getSupportFragmentManager().getFragment(savedInstanceState, TAG),
MyActivity.REQUEST_CODE);
}
return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// Retain our callback fragment, the TAG is just a key by which we later access the fragment
getActivity().getSupportFragmentManager().putFragment(outState, TAG, getTargetFragment());
}
public void onSave(){
//This gets called, when the fragment has done all its work and is ready to send the reply to Fragment 1
IActionListener callback = (IActionListener) getTargetFragment();
callback.doSomething();
}
}
现在将调用片段1中doSomething()的实现。下面是解决方案
请遵循以下步骤:
我创建这样的界面
public interface TitleChangeListener
{
public void onUpdateTitle(String title);
}
2.使用此接口实现您的活动
for.e.g
public class OrderDetail extends ActionBarActivity implements TitleChangeListener
3.在本活动中,创建onUpdate标题()
现在,在片段一中编写一些代码
public class OrderPendingDetail extends Fragment
{
private View rootView;
private Context context;
private OrderDetail orderDetail;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
rootView = inflater.inflate(R.layout.order_pending_detail, container, false);
context = rootView.getContext();
//here OrderDetail is the name of ActionBarActivity
orderDetail = (OrderDetail) context;
//here pass some text to second Fragment using button ClickListener
but_updateOrder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
{
// here call to Activity onUpdateTitle()
orderDetail.onUpdateTitle("bridal");
}
});
return rootView;
}
}
5.在第二个片段setTitle()中编写一些代码
在这个解决方案中,当你们点击片段一的按钮时,它会在第二个片段中显示值。
我希望它能对你有所帮助 使用接口与宿主活动通信,然后将数据从活动中传输到片段^no,不要这样做。。看这个。。看看我的编辑。。哦,好吧,我又一次误解了这个问题@skybolt,你可以用Bundle来做。。这是通行证data@Zyoo直接看我在文档中的帖子,我以为问题是针对dialogfragment回调的..如果我使用的是setRetainInstance(true)
?它还需要先传递到活动吗?是的。检查文档我认为您的第一段代码摘录中存在代码错误。您在某一点上调用WeakReference“ActionListener”,在另一点上调用“ActionCallback”。你能检查一下吗?@ParthShah噢,谢谢!在我的脑海中写下了这一点,但没有仔细检查变量名-再次感谢你,我纠正了它。尽管如此,答案还是非常好,有很多很好的例子!干得好!根据android开发者的原始文档,这种方法是错误的。我们应该使用活动在两个片段之间进行通信。以下是链接: