Intent intent = new Intent(Main.this, Fragment.class); startActivity(intent); // Не получается 

    2 answers 2

    This is how activities are triggered. Work with fragments is different. You can add <fragment> to the view file or work with fragments through the fragment manager. For example:

     FragmentTransaction fTrans = getFragmentManager().beginTransaction(); fTrans.add(R.id.frgmCont, frag1); 

    In general, for a better understanding, read this lesson and further on this site.

    It is impossible to open a fragment through Intent .

    In addition, it is impossible to make a "transition from activation to fragment" . A fragment is always associated with activity, it cannot exist separately from activity. So you can open fragments, replace fragments, but all this will go through the activity that "is the parent" of the fragment / fragments.

    • Why FragmentTransaction fTrans = getFragmentManager (). BeginTransaction (); highlighted by a completely red line - Developer Chingis
    • @DeveloperChingis, where do you write this? - Ksenia
    • to OnClickListener - Developer Chingis

    You can add a snippet to an activity using a transaction:

      FragmentTransaction ft = getFragmentManager().beginTransaction(); MyFragment fragment = new MyFragment(); ft.add(R.id.container, fragment); ft.commit(); 

    The first argument passed to the add() method is the ViewGroup container object for the fragment ( LinearLayout , for example), specified using the resource identifier. The second parameter is the fragment to add.
    The commit() method is called for the changes to take effect.

    • Why FragmentTransaction ft = getFragmentManager (). BeginTransaction (); highlighted by a completely red line - Developer Chingis
    • @Developer Chingis, I have no errors. And what error does he write about? - Real KEK
    • what import do you have? - Developer Chingis
    • @Developer Chingis, import android.app.FragmentTransaction; - Real KEK