Android:如何使用 Intent 从活动中调用 Fragment 类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22636722/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android: How to call Fragment class from activity using Intent
提问by Rajeev Sahu
I am trying to call a Fragment class from Activity using Intent. Is it possible to implement. Please provide your views.
我正在尝试使用 Intent 从 Activity 调用 Fragment 类。是否可以实施。请提供您的意见。
采纳答案by fllo
Fragment
needs to be hosted by a FragmentActivity
, you can't add a fragment via an Intent
.
Fragment
需要由 托管FragmentActivity
,您不能通过Intent
.
You need to create a FragmentManager
to add your fragment in the FragmentActivity
(or call another FragmentActivity
via an Intent
and add your fragment on it).
See this topic for more information: Add a Fragment to an Activity at Runtime.
您需要创建一个FragmentManager
以将您的片段添加到FragmentActivity
(或FragmentActivity
通过 an调用另一个Intent
并在其上添加您的片段)。
有关更多信息,请参阅此主题:在运行时向活动添加片段。
回答by Badamchi
Fragment TargetFragment=new target_fragment();
FragmentTransaction transaction=getFragmentManager().beginTransaction();
transaction.replace(R.id.main_content,TargetFragment);
transaction.addToBackStack(null);
transaction.commit();
回答by Sai Gopi N
Intent can not be applicable to Activity to Fragment So there Is Another method
Intent 不能应用于 Activity 到 Fragment 所以还有另一种方法
getSupportFragmentManager().beginTransaction().replace(R.id.container,new DashBoardFragment()).commit();