Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment does not show after inflating [Fixed, but...]

I'm trying to create a music player that will be visible in all my activities. To do this, I'm going to use a Fragment as some of you advised me earlier.

Since I have no experience with Fragments whatsoever, I decided to implement a fragment in a "filler" activity first. I created a simple fragment containing only a button and some text, and try to inflate that in my filler activity.

However, after inflating this fragment does not show up. A message does get printed to LogCat telling me that the fragment has been inflating.

I'm pretty sure I'm missing something, but since I have no experience with this and I couldn't find a tutorial that quite explained how to do this, I don't know what it is that I'm missing.

Music player Fragment

public class AppMusicPlayer extends Fragment{


     @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
         System.out.println("fragment added");
            return inflater.inflate(R.layout.musicplayer_main, container, false);
        }


}

Music Player layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >


    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is the fragment you're looking for" />

</LinearLayout>

Filler Activity

public class Filler extends FragmentActivity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.filler);

        Button b = (Button) findViewById(R.id.terug);
        b.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {
                Intent intent = new Intent();
                setResult(RESULT_OK, intent);
                finish();
            }
        });

    }

}

Filler layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
<!-- Title bar -->
    <RelativeLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        android:background="@drawable/title_bar" >

        <TextView
            android:id="@+id/fillertitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="Work In Progress"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textStyle="bold" >
        </TextView>

        <Button
            android:id="@+id/terug"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_centerVertical="true"
            android:background="@drawable/button_back"
            android:text="Back"
            android:textColor="#FFFFFFFF"
            android:textStyle="bold" >
        </Button>
    </RelativeLayout>
<!-- End of title bar -->
    <TextView
        android:id="@+id/wip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="This section of the app has not been made yet. Work in progress." >
    </TextView>

    <fragment
        android:id="@+id/musicPlayerFragment"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:name="com.mobowski.app.player.AppMusicPlayer" />

</LinearLayout>

Obviously I'm doing something wrong, but what?

Note: I'm using Android 2.3 with the Compatibility library.

Any help is appreciated


The problem has been fixed with the help of Yashwanth Kumar and blessenm. One gave an XML solution, the other a programmatic solution. Now I'm just wondering which solution is the most desirable one. Are there any concessions to be made with either of the solutions, or does it all boil down to the programmer's preferred solution?

like image 673
Sander van't Veer Avatar asked Dec 01 '11 10:12

Sander van't Veer


People also ask

How do you know if a fragment is destroyed?

Since all fragments are destroyed if the activity is destroyed, a simple answer could be calling getActivity(). isDestroyed() returning true if the activity is destroyed, therefore the fragment is destroyed.

Can a fragment exist without an activity?

While an activity can exist without a fragment, you cannot use a fragment without an activity. The UI fragments operate like a view within the activity's layout.

What happens to fragment when activity is destroyed?

As Fragment is embedded inside an Activity, it will be killed when Activity is killed. As contents of activity are first killed, fragment will be destroyed just before activity gets destroyed.

Are fragments reusable?

A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.


2 Answers

I haven't tried adding fragments directly to xml. But what I normally do is add a framelayout or linearlayout to the filler layout.

Suppose its id is 'fragment_holder'. I use the following code in the fragment activity right after setContentView. Try this

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.add(R.id.fragment_holder,new AppMusicPlayer(),"musicplayer");
ft.commit();
like image 148
blessanm86 Avatar answered Oct 10 '22 04:10

blessanm86


you should mention layout_height as 0dip, not the width in vertical linear layout.

change that it should work.

<fragment
        android:id="@+id/musicPlayerFragment"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:name="com.mobowski.app.player.AppMusicPlayer" />
like image 7
Yashwanth Kumar Avatar answered Oct 10 '22 06:10

Yashwanth Kumar