Android code that show difference between add(), replace() and addToBackStack()

In this android programming source code example, we are going to explore android code that show difference between add(), replace() and addToBackStack().

Below is a step by step source code that show difference between add(), replace() and addToBackStack() in android.

activity_main.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="#ededed">

    <FrameLayout
        android:id="@+id/frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/add"/>

    <Button
        android:id="@+id/add"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add Fragment"
        android:layout_above="@+id/addtobackstack"
        android:gravity="center"
        android:layout_marginBottom="1dp"
        android:background="@color/colorAccent"/>
    <Button
        android:id="@+id/addtobackstack"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Add To BackStack"
        android:layout_above="@+id/replace"
        android:gravity="center"
        android:layout_marginBottom="1dp"
        android:background="@color/colorAccent"/>
    <Button
        android:id="@+id/replace"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:text="Replace Fragment"
        android:gravity="center"
        android:background="@color/colorAccent"/>

</RelativeLayout>

MainActivity.java

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;


public class FragmentActivity18 extends AppCompatActivity {
    private String Tag = "home";
    private Button add;
    private Button addtobackstack;
    private Button replace;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment18);
        add = (Button)findViewById(R.id.add);
        addtobackstack = (Button)findViewById(R.id.addtobackstack);
        replace = (Button)findViewById(R.id.replace);
        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager mFragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                fragmentTransaction.add(R.id.frame,new Fragment18(), Tag).commit();
            }
        });

        addtobackstack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager mFragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                fragmentTransaction.add(R.id.frame,new Fragment18(), Tag).addToBackStack(null).commit();
            }
        });

        replace.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FragmentManager mFragmentManager = getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.frame,new Fragment18()).commit();
            }
        });

    }
}

fragment_fragment18.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="#ededed">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Fragment"
        android:gravity="center"
        android:textSize="20sp"/>

</RelativeLayout>

Fragment18.java

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

public class Fragment18 extends Fragment{

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_fragment18, container, false);

        return view;
    }

}

If you have any question or suggestions kindly use the comment box or you can contact us directly through our contact page below.

 

Add a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.