Fragment Transactions Example in Android

February 14, 2014




activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button_addfraga"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="add FragA" />

        <Button
            android:id="@+id/button_replacefragb"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Replace FragB" />
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <Button
            android:id="@+id/button_replacefragc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Replace FragC" />

        <Button
            android:id="@+id/button_removefragc"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Remove Fragc" />
    </TableRow>

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

</LinearLayout>

fraga.xml:
<?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"
    android:background="#ccffcc">

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is FragmentA"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

fragb.xml:
<?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"
     android:background="#ccffff">
   
 <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is FragmentB"
        android:textAppearance="?android:attr/textAppearanceLarge" />
   
</LinearLayout>

fragc.xml:
<?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"
     android:background="#ffffcc">
   
 <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Fragmentc"
        android:textAppearance="?android:attr/textAppearanceLarge" />
   
</LinearLayout>

FragA.java:
package com.ram.fragmenttransactions;

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

@SuppressLint("NewApi")
public class FragA extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fraga, container, false);
    }
}

FragB.java:
package com.ram.fragmenttransactions;

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

public class Fragb extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragb, container, false);
    }
}

FragC.java:
package com.ram.fragmenttransactions;

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

@SuppressLint("NewApi")
public class FragC extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.fragc, container, false);
    }
}

MainActivity.java:
package com.ram.fragmenttransactions;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

@SuppressLint("NewApi")
public class MainActivity extends Activity implements OnClickListener {
    Button b1, b2, b3, b4;
    FragmentManager fm;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        fm = getFragmentManager();

        b1 = (Button) findViewById(R.id.button_addfraga);
        b2 = (Button) findViewById(R.id.button_replacefragb);
        b3 = (Button) findViewById(R.id.button_replacefragc);
        b4 = (Button) findViewById(R.id.button_removefragc);

        b1.setOnClickListener(this);
        b2.setOnClickListener(this);
        b3.setOnClickListener(this);
        b4.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.button_addfraga:

            FragmentTransaction ft1 = fm.beginTransaction();
            ft1.add(R.id.fragArea, new FragA());
            ft1.commit();

            break;

        case R.id.button_replacefragb:

            FragmentTransaction ft2 = fm.beginTransaction();

            ft2.replace(R.id.fragArea, new Fragb());
            ft2.addToBackStack(null);
            ft2.commit();

            break;
        case R.id.button_replacefragc:
            FragmentTransaction ft3 = fm.beginTransaction();

            ft3.replace(R.id.fragArea, new FragC(), "fragc");
            ft3.addToBackStack(null);
            ft3.commit();
            break;
        case R.id.button_removefragc:
            FragmentTransaction ft4 = fm.beginTransaction();

            FragC fc = (FragC) fm.findFragmentByTag("fragc");

            ft4.remove(fc);
            ft4.commit();
            break;
        }
    }

}

Artikel Terkait

Previous
Next Post »