I have been away from Android app creation for quite a while and has just recently begun my new App.
Now i thought i would test out this new Android Studio and therefore downloaded it.
When i started my project it asked for a fragment
and i wasnt quite sure what it ment (i know what a fragment
is!).
When it started the project i had a activity_main
and a fragment_main
. And now every time i want to create a new activity
it requires me to create an additional fragement
.
Can anyone explain to me why this is happening (maybe ive missed something).
Fragments cannot live on their own--they must be hosted by an activity or another fragment. The fragment's view hierarchy becomes part of, or attaches to, the host's view hierarchy. Note: Some Android Jetpack libraries, such as Navigation, BottomNavigationView , and ViewPager2 , are designed to work with fragments.
If you want to start a new instance of mFragmentFavorite , you can do so via an Intent . Intent intent = new Intent(this, mFragmentFavorite. class); startActivity(intent); If you want to start aFavorite instead of mFragmentFavorite then you only need to change out their names in the created Intent .
A Fragment is not an Activity. Fragments are hosted inside a FragmentActivity.
You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.
Fragments are part of Google's UI design philosophy since Honeycomb (see embedded link).
If you would like to work around the default activity template in Android Studio, then check out this blog post.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With