Android,从片段活动到片段的返回结果如何?
我的碎片活动中有一个碎片。 为了显示片段对话框,我的片段中有一个接口,该接口已在片段活动中实现。 到目前为止没有问题。我可以弹出一个对话框。碎片活动能够在用户点击时获得OK/Cancel按钮Android,从片段活动到片段的返回结果如何?,android,android-fragments,Android,Android Fragments,我的碎片活动中有一个碎片。 为了显示片段对话框,我的片段中有一个接口,该接口已在片段活动中实现。 到目前为止没有问题。我可以弹出一个对话框。碎片活动能够在用户点击时获得OK/Cancel按钮 现在,我的问题是,是否有任何方法(替代方法,而不是在片段活动中创建另一个接口并在片段中实现它)让片段知道单击了OK/Cancel 由于您的活动应该知道它使用了哪些片段,您可以轻松获得您的片段,将其转换为它的真实类(如果您不需要新接口),并调用它的公共方法: ArticleFragment articleFr
现在,我的问题是,是否有任何方法(替代方法,而不是在片段活动中创建另一个接口并在片段中实现它)让片段知道单击了OK/Cancel 由于您的活动应该知道它使用了哪些片段,您可以轻松获得您的片段,将其转换为它的真实类(如果您不需要新接口),并调用它的公共方法:
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 {
// Otherwise, 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();
}
注意:此代码取自