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

एंड्रॉइड में पिछली एक्टिविटी को डेटा कैसे वापस भेजें?

startActivityForResult के साथ रिजल्ट वापस करना

जब दूसरी एक्टिविटी से पहली एक्टिविटी में डेटा वापस भेजना हो, तो startActivityForResult() और onActivityResult() का उपयोग करें। नोट: एंड्रॉइड X में यह डिप्रिकेटेड है; आधुनिक ऐप्स में Activity Result API (registerForActivityResult) उपयोग करें।

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

चरण 2: मेन एक्टिविटी लेआउट (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:layout_margin="16dp"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:gravity="center" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="16dp"
        android:text="डेटा प्राप्त करें" />
</RelativeLayout>

चरण 3: MainActivity.java

package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private static final int MY_REQUEST_CODE = 1;
    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        textView = findViewById(R.id.text_view);
        Button button = findViewById(R.id.button);

        button.setOnClickListener(v -> {
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            startActivityForResult(intent, MY_REQUEST_CODE);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK && requestCode == MY_REQUEST_CODE) {
            if (data != null) {
                String value = data.getStringExtra("value");
                textView.setText(value);
            }
        }
    }
}

चरण 4: सेकंड एक्टिविटी लेआउट (res/layout/activity_second.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:layout_margin="16dp"
    tools:context=".SecondActivity">

    <EditText
        android:id="@+id/edit_text"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:hint="पिछली एक्टिविटी को वापस भेजने के लिए कुछ दर्ज करें"
        android:inputType="text" />

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_marginTop="16dp"
        android:text="हो गया" />
</RelativeLayout>

चरण 5: SecondActivity.java

package com.example.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        EditText editText = findViewById(R.id.edit_text);
        Button button = findViewById(R.id.button);

        button.setOnClickListener(v -> {
            String value = editText.getText().toString().trim();
            Intent intent = new Intent();
            intent.putExtra("value", value);
            setResult(RESULT_OK, intent);
            finish();
        });
    }
}

चरण 6: AndroidManifest.xml

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

    <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=".SecondActivity"></activity>
    </application>
</manifest>

आधुनिक विकल्प: Activity Result API (अनुशंसित)

// MainActivity में (onCreate में)
ActivityResultLauncher<Intent> launcher = registerForActivityResult(
    new ActivityResultContracts.StartActivityForResult(),
    result -> {
        if (result.getResultCode() == Activity.RESULT_OK && result.getData() != null) {
            String value = result.getData().getStringExtra("value");
            textView.setText(value);
        }
    }
);

// लॉन्च करने के लिए:
launcher.launch(new Intent(MainActivity.this, SecondActivity.class));

यह तरीका अधिक सुरक्षित, टाइप-सेफ और लाइफसाइकल-अवेयर है।

  1. Android में मोबाइल डेटा कैसे डिसेबल करें? Android में मोबाइल डेटा कैसे डिसेबल करें?

    प्रोग्रामेटिक रूप से मोबाइल डेटा चालू/बंद करने का प्रयास - लेकिन गैर-रूटेड डिवाइस पर प्रतिबंधों के कारण यह काम नहीं करता। ⚠️ महत्वपूर्ण सीमाएं Android 5.0+ (API 21+): setMobileDataEnabled() रिफ्लेक्शन के माध्यम से भी कॉल नहीं किया जा सकता Android 2.1-4.4: रिफ्लेक्शन के माध्यम से काम करता था (रूटेड

  1. Android में एक Fragment से दूसरे Fragment में डेटा कैसे भेजें? Android में एक Fragment से दूसरे Fragment में डेटा कैसे भेजें?

    परिचय यह ट्यूटोरियल दर्शाता है कि Android में Fragment के बीच डेटा कैसे पास करें। हम ViewPager और TabLayout का उपयोग करके दो फ्रैगमेंट्स बनाएंगे और इंटरफेस के माध्यम से डेटा ट्रांसफर करेंगे। आवश्यक चरण चरण 1: नया प्रोजेक्ट बनाएं Android Studio में File → New Project चुनें। चरण 2: मुख्य लेआउट (res

  1. Android Data Binding मास्टर करें: सहज UI डेटा एकीकरण Android Data Binding मास्टर करें: सहज UI डेटा एकीकरण

    डेटा बाइंडिंग एक तकनीक है जिसका उपयोग तब किया जाता है जब आप सूचना के टुकड़ों (आपके डेटा) को कुछ विज़ुअल उपयोगकर्ता इनपुट तत्वों से जोड़ना चाहते हैं। इस प्रक्रिया में, जब भी इनपुट अपडेट होता है, तो उसके पीछे का डेटा भी अपडेट हो जाता है। यह कोई नई अवधारणा नहीं है, और ऐसे ढेर सारे फ्रेमवर्क हैं जिन्हें