chathura
chathura

Reputation: 3452

fragment_main and activity_main layouts in Android Studio

I am confused with the two layout files that are generated by the Android Studio. (fragment_main.xml and activity_main.xml)

I use activity_main.xml. To use activity_main.xml I need to comment out

if(savedInstanceState==null){...} //in onCreate(), Otherwise it will not display

Then I can use activity_main.xml.

Can some one explain me

  1. when to use Fragment_main.xml

  2. Advantage of using both layouts.

  3. How to use them correctly.(If I do not comment out above line it will not display activity_main.xml, instead it shows fragment_main layout. That means I need to create the interface in the fragment_main.xml.)

Thanks.

Upvotes: 5

Views: 14150

Answers (3)

Valynk
Valynk

Reputation: 477

It's always better using one layout. Which in this case I will suggest using activity_main.xml and delete the fragment_activity.xml following the below procedure:

1.Creat project normally.

2.Copy fragment_main.xml to activity_main.xml (content). Then delete fragment_main.xml

3.In MainActivity.java delete the following content :

if (savedInstanceState == null) {
    getFragmentManager().beginTransaction()
            .add(R.id.container, new PlaceholderFragment()).commit();
}

and

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

Hope this help

Upvotes: 0

Piyush Agarwal
Piyush Agarwal

Reputation: 25858

Following are the benefits using Fragments(Fragmenting your Activities) :

  1. Multiple mode supports : Like if your application supports landscape and portrait mode or tablet devices. Consider something like you have a list view and another is the details page in your app. You can make two fragments one with list view and one with details instead of two activities and can group together in landscape and in tablet devices instead of making an another activity for tablet. Have a look on android Settings screen in tablet.

  2. Custom Views : There are some scenarios where we need to create some custom views but the issue with custom views are maintaining the state so instead you can use fragment if they fits in your needs.

  3. App Navigation : Sliding drawer navigation handling. Open Google Play in your device an check the sliding navigation. If you tap on any of the option available in sliding navigation bar, you wont see any activity started on tapping on the items. As they all are top view of app,in such scenarios you can have an Activity with fragment and change them on tap .

  4. Reuse the views : Once you Create your Fragment you can use them in any activity at run time attaching them in your activity.

There are many other benefits, you will find them once you start using.

There is a nice explanation over here on android developer's space :

http://developer.android.com/guide/components/fragments.html

Read this and decide.

Upvotes: 6

Johan S
Johan S

Reputation: 3591

  1. I try always to use fragments in activities, I want my activities as small as possible, with all the important code contained in the fragment. This is the recommended way to go, check out this link.

  2. As I wrote, if your activity is very thin, e.g. showing an image then yeah sure, use only an activity but fragments have a ton of other benefits. Like rotating, the activity dies but the fragment stays alive!

  3. I don't really think you understand the concept of fragments. The link above states this:

A Fragment represents a behavior or a portion of user interface in an Activity.

If you don't want your fragment please check the activity_main.xml file and remove fragment reference, or just don't add it with fragment manager. But you seem pretty green so read about fragments, they make android programming a bit more tolerable.

Upvotes: 0

Related Questions