Consider the sample app from developers.android.com
This describes using Fragments like so:
Great! ... But... On the first example (the one with a phone) you create an Activity with an xml file containing a single <fragment>
and that's all, in the activity you only call setContentView()
on that xml? That seems like a lot of redundant code (Activity, XML & Fragment to display a Fragment): Can you set a Fragment
as an Activity
or is a Wrapper with XML always required?
After using multiple fragments in a single activity, we can create a multi-screen UI. Fragment cannot be used without an Activity. While Using fragments in the project, the project structure will be good and we can handle it easily.
Fragments introduce modularity and reusability into your activity's UI by allowing you to divide the UI into discrete chunks. Activities are an ideal place to put global elements around your app's user interface, such as a navigation drawer.
January 5, 2021. According to the Android documentation, a fragment is a part of applications user interface that is bound to an activity. Fragments have their lifecycle and layouts or UI components. Fragments help enrich your UI design, pass data between different screens, and adapt to different device configurations.
Fragments were introduced all the way back in Android Honeycomb to help alleviate the new screen sizes that had just been added thanks to the introduction of tablets. They help us reuse components into multiple activities, providing much of the same functionality, but doing a few things better.
Ah, found it here
public class MainMenuHolder extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // If not already added to the Fragment manager add it. If you don't do this a new Fragment will be added every time this method is called (Such as on orientation change) if(savedInstanceState == null) getSupportFragmentManager().beginTransaction().add(android.R.id.content, new MainMenuFragment()).commit(); } }
FragmentActivity allow's you to set the Fragment as the content of android.R.id.content
which I assume is the android internal ID of the trunk view.
With this method you still end up with an mostly redundant activity (If all you want is the Fragment acting as the Activity). But still, half as much fluff as having an activity and an XML file acting as a container.
Any other answers would be appreciated!
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