Declare a fragment
To simply declare a fragment that you can call later in your code:
class SecondFragSampleName : Fragment(R.layout.second_fragment)
or if you want to specify extra methods that will run when the fragment becomes active.
class SecondFragSampleName : Fragment(R.layout.second_fragment) { // Declare methods here, ie, OnCreate, etc. }
Notes:
- Do not declare this inside the main activity fragment. Suggested placement is after the main activity fragment.
- Ensure that a second_fragment layout exists, and that it contains the androidx.fragment.app.FragmentContainerView element.
Then when you want to switch to this "second fragment", add the following code as an action/method on the currently active fragment:
supportFragmentManager .beginTransaction() .setTransition(TRANSIT_FRAGMENT_OPEN) .setReorderingAllowed(true) .addToBackStack("DefaultView") .add(R.id.map, SecondFragSampleName()) .commit()
→