Category

Learn Android (Beginner to Expert) Urdu/Hindi

Category

Before start work to load Fragment in Android, we should know what is a Fragment? A fragment is a combination of XML and same like Java classes. A fragment is a portion in fragment Activity. We can also load fragment within fragment called child fragment.

A fragment has a lifecycle like Activity, for example, onCreate, onCreateView and onViewCreated, etc.

SupportFragmentManager uses to manage transaction between fragments. FragmentTransaction is a way to add, delete or remove the fragment. Similarly, SupportChildFragmentManager uses to manage transaction between child fragments.

Let’s start the project

First, create your fragment Java class for example

package com.becody.fragments;

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

import com.becody.myapplication.R;

/**
 * Created by Rana Aamer Sohail on 04,07,2019
 */
public class BlankFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_blank,container,false);
        return view;
    }
}

For loading fragment, you will require the container component. In your container components, you will add fragment. Now in my Java class, I have one button and one FrameLayout.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <Button
        android:id="@+id/btnClickMe"
        android:layout_width="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_height="wrap_content"
        android:text="Click Me"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

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

 

I create a Java method, In this method, first I will create an instance of FragmentTransaction class by using FragmentSupportManager.

 private void loadFrag(){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        BlankFragment blankFragment = new BlankFragment();
        ft.add(R.id.container,blankFragment);
        ft.commitAllowingStateLoss();
    }

Here is the full code of MainActivity Java class

 

package com.becody.myapplication;

import android.content.DialogInterface;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.becody.fragments.BlankFragment;

public class MainActivity extends AppCompatActivity {
    LocalStorage localStorage;
    private Button btnClickMe;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        localStorage = new LocalStorage(this);
        btnClickMe = findViewById(R.id.btnClickMe);
        btnClickMe.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                loadFrag();
            }
        });

    }

    private void loadFrag(){
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        BlankFragment blankFragment = new BlankFragment();
        ft.add(R.id.container,blankFragment);
        ft.commitAllowingStateLoss();
    }

}

 

thanks for reading my blog. if you still any confusion about this blog, you can watch the tutorial in Urdu and Hindi on my Youtube channel.

 

Download SourceCode