Android: How to call a fragment class from activity using Intent

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

+4
source share
3 answers

Fragmentmust be posted FragmentActivity, you cannot add fragment through Intent.

You need to create FragmentManagerto add a fragment to FragmentActivity(or call another FragmentActivitythrough Intentand add a fragment to it).
See This Topic for more information: Add Snippet to Action at Run Time .

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

Intention cannot be applied to Activity to Fragment. There is another method

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

Source: https://habr.com/ru/post/1533347/


All Articles