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

অ্যান্ড্রয়েডে ফ্র্যাগমেন্ট間 ডেটা পাস করার পদ্ধতি

দুই ফ্র্যাগমেন্টের মধ্যে ডেটা আদান-প্রদান করা

এই উদাহরণটি দেখাবে কীভাবে ইন্টারফেস ব্যবহার করে একটি ফ্র্যাগমেন্ট থেকে অন্য ফ্র্যাগমেন্টে ডেটা পাস করা যায়।

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

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"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <LinearLayout
        android:id="@+id/linearlayout01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ccc"
        android:layout_weight="1"
        android:orientation="vertical">
        <fragment
            android:name="com.example.myapplication.FirstFragment"
            android:id="@+id/frag_1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>
    <LinearLayout
        android:id="@+id/linearlayout02"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1"
        android:background="#eee"
        android:orientation="vertical">
        <fragment
            android:name="com.example.myapplication.SecondFragment"
            android:id="@+id/frag_2"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" />
    </LinearLayout>
</LinearLayout>

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

package com.example.myapplication;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public class MainActivity extends FragmentActivity implements OnButtonPressListener {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onButtonPressed(String msg) {
        SecondFragment obj = (SecondFragment) getSupportFragmentManager().findFragmentById(R.id.frag_2);
        obj.onFragmentInteraction(msg);
    }
}

ধাপ ৪: প্রথম ফ্র্যাগমেন্ট (src/FirstFragment.java)

package com.example.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class FirstFragment extends Fragment {
    OnButtonPressListener buttonListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment, null);
        init(root);
        return root;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            buttonListener = (OnButtonPressListener) getActivity();
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement onButtonPressed");
        }
    }

    void init(ViewGroup root) {
        TextView but = root.findViewById(R.id.text);
        but.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                buttonListener.onButtonPressed("Message From First Fragment");
            }
        });
    }
}

ধাপ ৫: দ্বিতীয় ফ্র্যাগমেন্ট (src/SecondFragment.java)

package com.example.myapplication;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class SecondFragment extends Fragment {
    TextView textView;
    View view;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment, container, false);
        return view;
    }

    public void onFragmentInteraction(String uri) {
        Log.d("sai", uri);
        textView = view.findViewById(R.id.text);
        textView.setText(uri);
    }
}

ধাপ ৬: ফ্র্যাগমেন্ট লেআউট (res/layout/fragment.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:gravity="center"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/text"
        android:textSize="30sp"
        android:text="Click"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

ধাপ ৭: লিসেনার ইন্টারফেস (src/OnButtonPressListener.java)

public interface OnButtonPressListener {
    public void onButtonPressed(String msg);
}

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

অ্যাপ চালু করুন। প্রথম ফ্র্যাগমেন্টের টেক্সটে ক্লিক করলে দ্বিতীয় ফ্র্যাগমেন্টে মেসেজ দেখাবে।

অ্যান্ড্রয়েডে ফ্র্যাগমেন্ট間 ডেটা পাস করার পদ্ধতি

অ্যান্ড্রয়েডে ফ্র্যাগমেন্ট間 ডেটা পাস করার পদ্ধতি

  1. অ্যান্ড্রয়েডে একটি অবজেক্ট এক এক্টিভিটি থেকে অন্য একে পাস করার পদ্ধতি

  2. অ্যান্ড্রয়েডে এক্টিভিটির থেকে ফ্র্যাগমেন্টে ভেরিয়েবল পাস করার পদ্ধতি

  3. অ্যান্ড্রয়েডে একটি ইমেজ এক्टीভিটি থেকে অন্য এক्टीভিটিতে পাস করার পদ্ধতি

  4. Android-এ একটি ফ্র্যাগমেন্ট থেকে অন্য ফ্র্যাগমেন্টে ডেটা পাস করার পদ্ধতি