परिचय
यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके एंड्रॉइड ऐप में फ्रैगमेंट से एक्टिविटी के मेथड को कैसे कॉल किया जाए।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।
चरण 2: मुख्य लेआउट XML कॉन्फ़िगर करें
res/layout/activity_main.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Tutorials Point"
android:textAlignment="center"
android:textColor="@android:color/holo_green_dark"
android:textSize="32sp"
android:textStyle="bold" />
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
चरण 3: MainActivity कोड
src/MainActivity.kt में निम्नलिखित कोड जोड़ें:
import android.os.Bundle
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val fragmentManager = supportFragmentManager
val fragmentTransaction = fragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.frameLayout, SampleFragment()).commit()
}
// फ्रैगमेंट से कॉल होने वाला मेथड
fun fragmentMethod() {
Toast.makeText(this@MainActivity, "फ्रैगमेंट से मेथड कॉल किया गया", Toast.LENGTH_LONG).show()
}
}
चरण 4: फ्रैगमेंट लेआउट बनाएं
res/layout/fragment_sample.xml नाम से एक नया लेआउट फाइल बनाएं:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SampleFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="फ्रैगमेंट से सिंपल मेथड कॉल करना"
android:textAlignment="center"
android:textColor="@android:color/background_dark"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 5: फ्रैगमेंट क्लास बनाएं
SampleFragment.kt नाम से एक नई Kotlin क्लास बनाएं:
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class SampleFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_sample, container, false)
// एक्टिविटी को कास्ट करके मेथड कॉल करें
(activity as? MainActivity)?.fragmentMethod()
return view
}
}
चरण 6: मैनिफेस्ट कॉन्फ़िगर करें
androidManifest.xml में निम्नलिखित कोड जोड़ें:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.q11">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
ऐप चलाएं
अपने एंड्रॉइड डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में प्रोजेक्ट की एक्टिविटी फाइल खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप का डिफॉल्ट स्क्रीन देखें।
बेहतर विकल्प: इंटरफेस का उपयोग करें
टाइट कपलिंग से बचने के लिए, इंटरफेस पैटर्न का उपयोग करें:
// इंटरफेस परिभाषित करें
interface OnFragmentInteractionListener {
fun onFragmentAction()
}
// एक्टिविटी में इम्प्लीमेंट करें
class MainActivity : AppCompatActivity(), OnFragmentInteractionListener {
override fun onFragmentAction() {
// हैंडल करें
}
}
// फ्रैगमेंट में उपयोग करें
class SampleFragment : Fragment() {
private var listener: OnFragmentInteractionListener? = null
override fun onAttach(context: Context) {
super.onAttach(context)
if (context is OnFragmentInteractionListener) {
listener = context
}
}
override fun onCreateView(...): View? {
listener?.onFragmentAction()
return view
}
}