Android Fragment क्या है? | Fragment Example in Android Studio (Hindi)

Android Fragments क्या हैं? (Full Explanation in Hindi)

Fragment Android का एक महत्वपूर्ण UI component है जो किसी Activity का modular part होता है। Fragment को आप sub-activity भी कह सकते हैं। यह reusable UI और logic create करने के लिए use होता है।

Fragment का Use क्यों होता है?

  • Large screen devices (Tablet) में multi-pane UI बनाने के लिए।
  • Reusable UI components के लिए।
  • Dynamic UI बनाने के लिए।

Fragment की Key Points

  • Fragment हमेशा किसी Activity के अंदर attach होता है।
  • Fragment का अपना lifecycle होता है।
  • Fragment का UI XML या dynamically add किया जा सकता है।

Fragment Lifecycle

  1. onAttach() → जब Fragment Activity से attach होता है।
  2. onCreate() → Fragment create होने पर।
  3. onCreateView() → Fragment का UI create होने पर।
  4. onStart() → जब Fragment visible होता है।
  5. onResume() → जब Fragment interactive होता है।
  6. onPause() → जब Fragment pause होता है।
  7. onDestroyView() → Fragment का UI destroy होने पर।
  8. onDestroy() → Fragment destroy होने पर।
  9. onDetach() → जब Fragment Activity से detach होता है।

Fragment के Types

  • Static Fragment → XML layout में define होते हैं।
  • Dynamic Fragment → Code के through runtime पर add/remove होते हैं।

Example: Static Fragment in Android Studio

Step 1: Fragment Layout (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"
    android:background="#E3F2FD">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello from Fragment!"
        android:textSize="20sp"
        android:textColor="#000" />

</LinearLayout>
    

Step 2: Fragment Class (ExampleFragment.java)

public class ExampleFragment extends Fragment {
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_example, container, false);
    }
}
    

Step 3: Add Fragment in Activity Layout (activity_main.xml)

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
    

Step 4: Load Fragment Dynamically (MainActivity.java)

getSupportFragmentManager().beginTransaction()
    .replace(R.id.fragment_container, new ExampleFragment())
    .commit();
    
✅ Output: App launch होने पर ExampleFragment load होगा और TextView में “Hello from Fragment!” दिखाई देगा।

Real Life Example

  • WhatsApp → Chats, Status, Calls tabs fragments हैं।
  • Instagram → Home, Search, Reels, Profile → fragments।
© 2025 LearnWithRehan Android Tutorials in Hindi - All Rights Reserved

Comments

Popular posts from this blog

Git And GitHub Collaborators and teams

🎯 Retrofit से API Call कैसे करें – Android Studio में (Java के साथ Step-by-Step गाइड)

How to create React JS application