Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragments - Do you have to use an Activity Wrapper around a fragment which comprises the whole Activity?

Tags:

Consider the sample app from developers.android.com

This describes using Fragments like so:

  • On a Phone you can use Fragment 1 on Activity A and fragment 2 on Activity B.
  • On a tablet you have more real estate so you use Fragment 1 and Fragment 2 on Activity A.

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?

like image 861
Graeme Avatar asked Jul 13 '11 09:07

Graeme


People also ask

Can fragments be used without an activity?

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.

What is the purpose of using fragments in an activity?

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.

When should Fragments be used?

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.

What is a fragment and what is an advantage of using fragments?

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.


1 Answers

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!

like image 162
Graeme Avatar answered Oct 12 '22 16:10

Graeme