Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Studio create new activity requires fragment

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).

like image 586
Marc Rasmussen Avatar asked Nov 30 '13 10:11

Marc Rasmussen


People also ask

Does an activity need a fragment?

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.

Can you start a new activity from a fragment?

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 .

Is fragment the child of an activity?

A Fragment is not an Activity. Fragments are hosted inside a FragmentActivity.

Can I go fragment to activity in Android?

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.


1 Answers

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.

like image 69
Luis Avatar answered Oct 09 '22 10:10

Luis