BCA / B.Tech 12 min read

Fragments के साथ काम करना

Unit 2 - Topic 12: Fragments के साथ काम करना

Fragment क्या है? (What is a Fragment?)

एक Fragment एक Activity के अंदर एक "mini-activity" की तरह होता है। यह User Interface (UI) का एक reusable हिस्सा है जिसका अपना layout और lifecycle होता है। आप एक single activity में multiple fragments को combine करके multi-pane UI बना सकते हैं।

  • सोचिए: एक Activity एक घर है, और Fragments उस घर के अलग-अलग कमरे हैं (जैसे living room, kitchen)। हर कमरे का अपना purpose और layout है, लेकिन वे सभी एक ही घर का हिस्सा हैं।

Fragment का उपयोग क्यों करें? (Why Use Fragments?)

  • Modularity : आप जटिल UI को छोटे, manageable parts में तोड़ सकते हैं।
  • Reusability : आप एक ही fragment को different activities में reuse कर सकते हैं।
  • Adaptability : आप different screen sizes के लिए अलग-अलग fragment combinations बना सकते हैं। उदाहरण के लिए, एक tablet पर आप दो fragments को side-by-side दिखा सकते हैं, जबकि phone पर उन्हें अलग-अलग screens पर दिखा सकते हैं।

Fragment Lifecycle

Fragment का अपना lifecycle होता है, जो उसकी host activity के lifecycle से जुड़ा होता है।

[Image of Android Fragment Lifecycle diagram]

मुख्य Lifecycle Methods:

  1. onAttach(): Fragment को activity से जोड़ा जाता है।
  2. onCreate(): Fragment को initialize किया जाता है।
  3. onCreateView(): Fragment के UI को बनाने और दिखाने के लिए यह method call होता है। यहाँ आप अपनी layout file को inflate करते हैं। यह सबसे महत्वपूर्ण methods में से एक है।
  4. onViewCreated(): `onCreateView()` के बाद call होता है, जब fragment का view बन चुका होता है।
  5. onStart(): Fragment user को visible हो जाता है।
  6. onResume(): Fragment active और interactive हो जाता है।
  7. onPause(): User fragment से दूर navigate कर रहा है।
  8. onStop(): Fragment visible नहीं है।
  9. onDestroyView(): Fragment के UI से जुड़े resources को clean up किया जाता है।
  10. onDestroy(): Fragment का final cleanup होता है।
  11. onDetach(): Fragment को activity से अलग कर दिया जाता है।

Fragment कैसे बनाएं (How to Create a Fragment)

Fragment बनाने के लिए दो main files की जरूरत होती है:

1. Java/Kotlin Class File: यह fragment के logic को control करती है।

Java में उदाहरण (`ExampleFragment.java`):


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

public class ExampleFragment extends Fragment {

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

2. XML Layout File: यह fragment के UI को define करती है।

उदाहरण (`fragment_example.xml`):


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is a Fragment"
        android:textSize="24sp" />

</LinearLayout>

Activity में Fragment कैसे Add करें

Fragments को एक activity में जोड़ने के दो तरीके हैं:

1. Statically (XML Layout का उपयोग करके):

आप activity की layout file में `` tag का उपयोग कर सकते हैं।


<!-- activity_main.xml -->
<fragment
    android:id="@+id/example_fragment"
    android:name="com.example.myapp.ExampleFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

2. Dynamically (Java Code का उपयोग करके):

यह अधिक flexible तरीका है। आप app के चलने के दौरान fragments को add, remove, या replace कर सकते हैं। यह `FragmentManager` और `FragmentTransaction` का उपयोग करके किया जाता है।

Java में उदाहरण (`MainActivity.java`):


// onCreate() method के अंदर

// 1. FragmentManager प्राप्त करें
FragmentManager fragmentManager = getSupportFragmentManager();

// 2. FragmentTransaction शुरू करें
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

// 3. Fragment का एक instance बनाएं
ExampleFragment fragment = new ExampleFragment();

// 4. Fragment को add/replace करें
// R.id.fragment_container एक FrameLayout है activity की layout में
fragmentTransaction.replace(R.id.fragment_container, fragment);

// 5. Transaction को commit करें
fragmentTransaction.commit();