Computer >> कंप्यूटर ट्यूटोरियल >  >> प्रोग्रामिंग >> Android

Kotlin का उपयोग करके Android में एक्टिविटीज के बीच ड्रॉएबल कैसे पास करें?

यह उदाहरण दर्शाता है कि Kotlin का उपयोग करके Android में एक एक्टिविटी से दूसरी एक्टिविटी में ड्रॉएबल (इमेज रिसोर्स) कैसे पास किया जाता है। रिसोर्स ID (Int) को Intent Extra के माध्यम से पास करना सबसे कुशल तरीका है।

चरण 1: नया प्रोजेक्ट बनाएं

Android Studio खोलें, File → New Project पर जाएं और नया प्रोजेक्ट बनाने के लिए सभी आवश्यक विवरण भरें।

चरण 2: activity_main.xml में UI जोड़ें

<?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=".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" />

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Transfer Bitmap to 2nd Activity" />

</RelativeLayout>

चरण 3: MainActivity.kt में इंटेंट के साथ रिसोर्स ID पास करें

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        title = "KotlinApp"

        val button: Button = findViewById(R.id.button)
        button.setOnClickListener {
            val intent = Intent(this@MainActivity, NewActivity::class.java)
            intent.putExtra("picture", R.drawable.ic_baseline_directions_walk_24)
            startActivity(intent)
        }
    }
}

चरण 4: दूसरी एक्टिविटी बनाएं (NewActivity.kt)

import android.os.Bundle
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity

class NewActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_new)
        title = "KotlinApp"

        val imageView: ImageView = findViewById(R.id.imageView)
        val bundle = intent.extras
        if (bundle != null) {
            val picture = bundle.getInt("picture")
            imageView.setImageResource(picture)
        }
    }
}

चरण 5: activity_new.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=".NewActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="25sp"
        android:text="Second Activity"
        android:textSize="16sp"
        android:textStyle="bold" />

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true" />

</RelativeLayout>

चरण 6: AndroidManifest.xml में दोनों एक्टिविटी घोषित करें

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.example.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>

        <activity android:name=".NewActivity" />

    </application>
</manifest>

ऐप चलाएं

अपने Android डिवाइस को कंप्यूटर से कनेक्ट करें। Android Studio में किसी एक्टिविटी फ़ाइल को खोलें और टूलबार से Run आइकन पर क्लिक करें। अपना मोबाइल डिवाइस चुनें और ऐप चलने पर डिफ़ॉल्ट स्क्रीन देखें।

वैकल्पिक तरीके:

  • Bitmap पास करना (नहीं सुझाया): intent.putExtra("bitmap", bitmap) - Parcelable size limit (~1MB) के कारण क्रैश हो सकता है
  • URI पास करना: intent.putExtra("uri", imageUri) - कंटेंट प्रोवाइडर/फाइल URI के लिए
  • ग्लोबल कैश/सिंगलटन: एप्लिकेशन क्लास में Bitmap कैश करें
  • FileProvider: फाइल URI शेयर करने के लिए सुरक्षित तरीका
  1. एंड्रॉइड में फ्रैगमेंट्स के बीच डेटा कैसे पास करें? एंड्रॉइड में फ्रैगमेंट्स के बीच डेटा कैसे पास करें?

    एंड्रॉइड में फ्रैगमेंट्स के बीच डेटा पास करने का सही तरीका एंड्रॉइड में दो फ्रैगमेंट्स के बीच सीधा कम्युनिकेशन **नहीं** करना चाहिए। Google की आधिकारिक गाइडलाइन के अनुसार, **एक्टिविटी को मध्यस्थ (mediator)** बनाना चाहिए। यह ट्यूटोरियल उसी पैटर्न को दर्शाता है: FragmentOne → Interface → MainActivity →

  1. Android में ड्रॉएबल (Drawable) को एक्टिविटी के बीच कैसे पास करें? Android में ड्रॉएबल (Drawable) को एक्टिविटी के बीच कैसे पास करें?

    Android में Intent के माध्यम से ड्रॉएबल संसाधन पास करने का गाइड यह ट्यूटोरियल दिखाता है कि कैसे ड्रॉएबल रिसोर्स ID (इंटीजर) को Intent extra के रूप में दूसरी एक्टिविटी में पास किया जाए और वहां ImageView में सेट किया जाए। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनकर नया प्रो

  1. Android में एक्टिविटी के बीच स्विच कैसे करें? Android में एक्टिविटी के बीच स्विच कैसे करें?

    एक्टिविटी के बीच नेविगेशन यह उदाहरण दर्शाता है कि Intent का उपयोग करके दो एक्टिविटी के बीच आगे-पीछे कैसे नेविगेट करें। चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: मुख्य लेआउट (activity_main.xml) <?xml version=1.0 encoding=utf-8?> <RelativeLayout xmlns:an