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

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


ভূমিকা

এই উদাহরণটি দেখাবে কিভাবে Bundle এবং setArguments() ব্যবহার করে এক্টিভিটির থেকে ফ্র্যাগমেন্টে ডেটা (স্ট্রিং) পাস করতে হয়।

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

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:tools="https://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:gravity="center" 
    android:padding="2dp" 
    tools:context=".MainActivity"> 
    <EditText 
        android:id="@+id/editText" 
        android:layout_marginTop="20sp" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:hint="ENTER YOUR MESSAGE" /> 
    <Button 
        android:id="@+id/btnSendData" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="Send data" /> 
    <FrameLayout 
        android:id="@+id/frameLayout" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"> 
    </FrameLayout> 
</LinearLayout>

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

import android.support.v4.app.FragmentManager; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 

public class MainActivity extends AppCompatActivity { 
    EditText editText; 
    Button button; 

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

        FragmentManager fragmentManager = getSupportFragmentManager(); 
        final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
        final MyFragment myFragment = new MyFragment(); 

        button = findViewById(R.id.btnSendData); 
        editText = findViewById(R.id.editText); 

        button.setOnClickListener(new View.OnClickListener() { 
            @Override 
            public void onClick(View v) { 
                Bundle b = new Bundle(); 
                b.putString("message", editText.getText().toString()); 
                myFragment.setArguments(b); 
                fragmentTransaction.add(R.id.frameLayout, myFragment).commit(); 
            } 
        }); 
    } 
}

ধাপ ৪: ফ্র্যাগমেন্ট ক্লাস (MyFragment.java)

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

public class MyFragment extends Fragment { 
    TextView textView; 

    public MyFragment() { } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
        View view = inflater.inflate(R.layout.fragment_my, container, false); 
        textView = view.findViewById(R.id.textView); 
        
        Bundle bundle = getArguments(); 
        String message = bundle.getString("message"); 
        textView.setText(message); 
        
        return view; 
    } 
}

ধাপ ৫: ফ্র্যাগমেন্ট লেআউট (fragment_my.xml)

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

ধাপ ৬: মেনিফেস্ট ফাইল (androidManifest.xml)

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

অ্যাপ্লিকেশন চালান

অ্যাপটি চালান। EditText-এ মেসেজ লিখে "Send data" বাটনে ক্লিক করলে ফ্র্যাগমেন্ট লোড হবে এবং পাঠানো মেসেজটি দেখাবে।


  1. অ্যান্ড্রয়েডে এক্টিভিটি থেকে রিংটোন সেট করা

  2. অ্যান্ড্রয়েডে এক্টিভিটির মাঝপথে Drawable পাস করা

  3. অ্যান্ড্রয়েডে ফ্র্যাগমেন্ট থেকে এক্টিভিটি মেথড কল করা

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