获得第二个片段数据结果 [英] Get data result from second Fragment
问题描述
在我MainActivity延伸FragmentActivity,我有一个碎裂,当我preSS在碎裂的按钮时,我打电话给FragmentB。
FragmentB F = FragmentB.newInstance(1);
。getSupportFragmentManager()调用BeginTransaction()取代(R.id.llMain,F).addToBackStack(空).commit()。
在FragmentB,我创建了一个对象的人P1(有姓名和年龄)。当我在FragmentB preSS一个按钮B,我称之为
getFragmentManager()popBackStack()。
它将返回碎裂,
所以,我想通过数据对象的人从P1到FragmentB碎裂。我需要做什么?
我试图寻找,但无法找到一个解决方案。
在您的片段创建回调和FragmentActivity处理它,
谷歌的例子有这种认识
宣布OnHeadlineSelectedListener回调
块引用>公共类HeadlinesFragment扩展ListFragment {
OnHeadlineSelectedListener mCallback;// Container活动必须实现此接口使FRAG可以传递消息
公共接口OnHeadlineSelectedListener {
通过调用HeadlinesFragment / **当选择列表项* /
公共无效onArticleSelected(INT位置);
}@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); //我们需要使用不同的列表项布局设备年纪比蜂窝
INT布局= Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB?
android.R.layout.simple_list_item_activated_1:android.R.layout.simple_list_item_1; //创建列表视图的阵列适配器,使用存有头条阵列
setListAdapter(新ArrayAdapter<串GT;(getActivity(),布局,Ipsum.Headlines));
}
@覆盖
公共无效onAttach(活动活动){
super.onAttach(活动); //这将确保集装箱活动实施
//回调接口。如果不是,它抛出一个异常。
尝试{
mCallback =(OnHeadlineSelectedListener)活性;
}赶上(抛出ClassCastException E){
抛出新ClassCastException异常(activity.toString()
+必须实现OnHeadlineSelectedListener);
}
}@覆盖
公共无效onListItemClick(ListView中升,视图V,INT位置,长的id){
//通知所选项目的父活动
mCallback.onArticleSelected(位置); //设置项为选中的两窗格布局中突出时,
getListView()setItemChecked(位置,真)。
}
从HeadLinesFragment实现回调方法FragmentActivity和发送(由.setArguments())数据ArticleFragment,如果ArticleFragment可用
块引用>公共类MainActivity扩展FragmentActivity
实现HeadlinesFragment.OnHeadlineSelectedListener {/ **当第一次创建活动调用。 * /
@覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.news_articles); //检查活动是否使用与布局版本
//将fragment_container的FrameLayout。如果是这样,我们必须添加的第一个片段
如果(findViewById(R.id.fragment_container)!= NULL){ //但是,如果我们被从previous状态恢复,
//那么我们不需要做任何事情,应该返回要不然
//我们可以重叠的片段结束。
如果(savedInstanceState!= NULL){
返回;
} //创建ExampleFragment的一个实例
HeadlinesFragment firstFragment =新HeadlinesFragment(); //如果这项活动开始由一个Intent特殊说明,
//通过意向的群众演员的片段作为参数
firstFragment.setArguments(getIntent()getExtras()); //片段添加到fragment_container的FrameLayout
getSupportFragmentManager()调用BeginTransaction()
。新增(R.id.fragment_container,firstFragment).commit();
}
}公共无效onArticleSelected(INT位置){
//用户选择的文章的标题从HeadlinesFragment //捕获从活动布局的文章片段
ArticleFragment articleFrag =(ArticleFragment)
。getSupportFragmentManager()findFragmentById(R.id.article_fragment); 如果(articleFrag!= NULL){
//如果文章FRAG是可用的,我们在两窗格布局... //在ArticleFragment调用一个方法来更新其内容
articleFrag.updateArticleView(位置); }其他{
//如果断枝不可用,我们是在一个窗格布局和必须交换断枝... //创建片段,并给它一个参数所选文章
ArticleFragment newFragment =新ArticleFragment();
捆绑ARGS =新包();
args.putInt(ArticleFragment.ARG_POSITION,位置);
newFragment.setArguments(参数);
FragmentTransaction交易= getSupportFragmentManager()调用BeginTransaction()。 //替换无论是在fragment_container认为这个片段,
//和事务添加到背堆栈,以便用户可以导航回
transaction.replace(R.id.fragment_container,newFragment);
transaction.addToBackStack(NULL); //提交事务
器transaction.commit();
}
}In my MainActivity extends FragmentActivity, I have a FragmentA, When I press a Button in FragmentA, I call to FragmentB.
FragmentB f = FragmentB.newInstance(1); getSupportFragmentManager().beginTransaction().replace(R.id.llMain, f).addToBackStack(null).commit();
In FragmentB, I create a Object People p1(with Name and age) . And When I press a Button B in FragmentB, I call
getFragmentManager().popBackStack();
It will return FragmentA,
So, I want to pass data Object People p1 from FragmentB to FragmentA. What do i have to do? I try to search but can't find a solution.
解决方案create CallBack in your Fragment and handle it in FragmentActivity, google example has this realization
declaring OnHeadlineSelectedListener callback
public class HeadlinesFragment extends ListFragment { OnHeadlineSelectedListener mCallback; // The container Activity must implement this interface so the frag can deliver messages public interface OnHeadlineSelectedListener { /** Called by HeadlinesFragment when a list item is selected */ public void onArticleSelected(int position); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // We need to use a different list item layout for devices older than Honeycomb int layout = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB ? android.R.layout.simple_list_item_activated_1 : android.R.layout.simple_list_item_1; // Create an array adapter for the list view, using the Ipsum headlines array setListAdapter(new ArrayAdapter<String>(getActivity(), layout, Ipsum.Headlines)); } @Override public void onAttach(Activity activity) { super.onAttach(activity); // This makes sure that the container activity has implemented // the callback interface. If not, it throws an exception. try { mCallback = (OnHeadlineSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener"); } } @Override public void onListItemClick(ListView l, View v, int position, long id) { // Notify the parent activity of selected item mCallback.onArticleSelected(position); // Set the item as checked to be highlighted when in two-pane layout getListView().setItemChecked(position, true); }
Realize callback method in FragmentActivity and send (by .setArguments()) data from HeadLinesFragment to ArticleFragment, if ArticleFragment is available
public class MainActivity extends FragmentActivity implements HeadlinesFragment.OnHeadlineSelectedListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.news_articles); // Check whether the activity is using the layout version with // the fragment_container FrameLayout. If so, we must add the first fragment if (findViewById(R.id.fragment_container) != null) { // However, if we're being restored from a previous state, // then we don't need to do anything and should return or else // we could end up with overlapping fragments. if (savedInstanceState != null) { return; } // Create an instance of ExampleFragment HeadlinesFragment firstFragment = new HeadlinesFragment(); // In case this activity was started with special instructions from an Intent, // pass the Intent's extras to the fragment as arguments firstFragment.setArguments(getIntent().getExtras()); // Add the fragment to the 'fragment_container' FrameLayout getSupportFragmentManager().beginTransaction() .add(R.id.fragment_container, firstFragment).commit(); } } public void onArticleSelected(int position) { // The user selected the headline of an article from the HeadlinesFragment // Capture the article fragment from the activity layout ArticleFragment articleFrag = (ArticleFragment) getSupportFragmentManager().findFragmentById(R.id.article_fragment); if (articleFrag != null) { // If article frag is available, we're in two-pane layout... // Call a method in the ArticleFragment to update its content articleFrag.updateArticleView(position); } else { // If the frag is not available, we're in the one-pane layout and must swap frags... // Create fragment and give it an argument for the selected article ArticleFragment newFragment = new ArticleFragment(); Bundle args = new Bundle(); args.putInt(ArticleFragment.ARG_POSITION, position); newFragment.setArguments(args); FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack so the user can navigate back transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit(); } }
这篇关于获得第二个片段数据结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!