0

i added the fragment using XML and it worked fine , but when i tried to add it dynamically it did not appear in the main activity

FragmentActivity.java:

package com.example.pc.learn_again;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.PersistableBundle;

public class FragmentActivity2 extends Activity{
@Override
public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
    super.onCreate(savedInstanceState, persistentState);
    setContentView(R.layout.mainfragment2);

    //adding the fragment using Java code---dynamically ya3ni//
    //create an object of the intended frag//
    Fragment3 frag = new Fragment3();
    //get the manager
    FragmentManager manager = getFragmentManager();
    //start the transaction
    FragmentTransaction transaction = manager.beginTransaction();
    //add to the layout
    transaction.add(R.id.mylayout,frag,"fragment3");//--note: the parameters used are 1- the id of the main layout 2- the intended fragment 3- a key for future use//
    //finish using the below
    transaction.commit();
}
}

Fragment3.java:

package com.example.pc.learn_again;


import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

public class Fragment3 extends Fragment {

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

the fragment3.xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/highlighted_text_material_light">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:id="@+id/textView3"
    android:layout_gravity="center_horizontal"
    android:layout_margin="40dp"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="New Button"
    android:id="@+id/button12"
    android:layout_below="@+id/textView3"
    android:layout_margin="40dp"
    android:layout_gravity="center_horizontal" />
</RelativeLayout>

the mainfragment2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mylayout">

</LinearLayout>

i checked all of the above and the connection between the xml files and the classes and all are correct though when i test it the fragment does not show in the activity

  • checked this? http://stackoverflow.com/questions/18296868/how-to-add-a-fragment-to-a-programmatically-generated-layout – Daniel Bo Sep 08 '15 at 15:41
  • Some things that I'm seeing, without testing your code: - try to import android.support.v4.* classes for your FragmentManager, FragmentTransaction and Fragment - try to change your activity to extend FragmentActivity - use different onCreate() method - the one without PersistentState – Tomislav Turcic Sep 08 '15 at 15:52

3 Answers3

0

Try this:

mainfragment2.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/mylayout"
    </LinearLayout>
</LinearLayout>
reviver
  • 168
  • 1
  • 8
0

I think the issue might be that you're using the wrong onCreate(), so your activity code never runs.

onCreate(Bundle) is the standard method which is always called when an activity is created. onCreate(Bundle, PersistableBundle) is only called if you have the persistableMode tag set correctly.

RussHWolf
  • 3,555
  • 1
  • 19
  • 26
0

Create a FrameLayout inside LinearLayout in mainfragment2.xml first:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/mylayout">

    <FrameLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container" />

</LinearLayout>

Then, place Fragment3 into this FrameLayout using replace() method:

getFragmentManager().beginTransaction()
        .replace(R.id.container, new Fragment3).commit();

Since Fragment was added in API level 11, you can use getSupportFragmentManager() which provided by Support Library to the make pre-Honeycomb devices support Fragment too.

Anggrayudi H
  • 14,977
  • 11
  • 54
  • 87