Android: How to call Fragment class from activity using Intent

孤街醉人 提交于 2019-11-28 04:33:57

问题


I am trying to call a Fragment class from Activity using Intent. Is it possible to implement. Please provide your views.


回答1:


Fragment needs to be hosted by a FragmentActivity, you can't add a fragment via an 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.




回答2:


            Fragment TargetFragment=new target_fragment();
            FragmentTransaction transaction=getFragmentManager().beginTransaction();
            transaction.replace(R.id.main_content,TargetFragment);
            transaction.addToBackStack(null);
            transaction.commit();



回答3:


Intent can not be applicable to Activity to Fragment So there Is Another method

getSupportFragmentManager().beginTransaction().replace(R.id.container,new DashBoardFragment()).commit();


来源:https://stackoverflow.com/questions/22636722/android-how-to-call-fragment-class-from-activity-using-intent

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!