কম্পিউটার টিউটোরিয়াল

অ্যান্ড্রয়েডে startActivityForResult পরিচালনা করা

startActivityForResult এবং onActivityResult ব্যবহার করে ডেটা ফেরত পাওয়া

এই উদাহরণটি দেখাবে কীভাবে একটি এক্টিভিটি থেকে অন্য এক্টিভিটি চালু করে ফলাফল ফেরত পাওয়া যায়।

ধাপ ১: নতুন প্রজেক্ট তৈরি করুন

Android Studio-তে File → New Project নির্বাচন করুন।

ধাপ ২: লেআউট ফাইল (res/layout/activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical">
    <TextView
        android:id="@+id/actionEvent"
        android:textSize="40sp"
        android:layout_marginTop="30dp"
        android:layout_width="wrap_content"
        android:layout_height="match_parent" />
</LinearLayout>

ধাপ ৩: মেইন এক্টিভিটি (src/MainActivity.java)

package com.example.myapplication;

import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    TextView actionEvent;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        actionEvent = findViewById(R.id.actionEvent);
        actionEvent.setText("Click");
        actionEvent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this, SecondActivity.class);
                startActivityForResult(i, 1);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (resultCode == RESULT_OK) {
                String returnString = data.getStringExtra("result");
                actionEvent.setText(returnString);
            }
        }
    }
}

ধাপ ৪: সেকেন্ড এক্টিভিটি (src/SecondActivity.java)

package com.example.myapplication;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);
        Intent returnIntent = new Intent();
        returnIntent.putExtra("result", "data from secondActivity");
        setResult(Activity.RESULT_OK, returnIntent);
        finish();
    }
}

ধাপ ৫: সেকেন্ড এক্টিভিটি লেআউট (activity_second.xml)

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".SecondActivity">
</android.support.constraint.ConstraintLayout>

ধাপ ৬: ম্যানিফেস্টে সেকেন্ড এক্টিভিটি যোগ করুন

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

অ্যাপ্লিকেশন চালানোর পদ্ধতি

অ্যাপ চালু করুন। টেক্সট ভিউতে ক্লিক করলে সেকেন্ড এক্টিভিটি খুলবে এবং তা থেকে ডেটা ফেরত পাবে।

অ্যান্ড্রয়েডে startActivityForResult পরিচালনা করা

অ্যান্ড্রয়েডে startActivityForResult পরিচালনা করা

  1. Android-এ Stack সর্ট (Sort) করার পদ্ধতি

  2. আপনার অ্যান্ড্রয়েড লোকেশন সেটিংস পরিচালনার পূর্ণাঙ্গ গাইড

  3. অ্যান্ড্রয়েডে নোটিফিকেশন ম্যানেজমেন্ট: সম্পূর্ণ গাইড

  4. অ্যান্ড্রয়েড ফাইল ম্যানেজমেন্টে মাস্টার হন: সাজান, এক্সেস করুন এবং কার্যকরভাবে স্টোর করুন