এন্ড্রয়েডে অ্যাক্টিভিটি রিস্টার্ট করার ধাপাবলী
এই উদাহরণটি দেখায় কীভাবে একটি অ্যাক্টিভিটিকে প্রোগ্রামেটিক্যালি রিস্টার্ট করা যায়। `finish()` কল করে বর্তমান অ্যাক্টিভিটি বন্ধ করে `startActivity(getIntent())` দিয়ে আবার শুরু করা হয়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project এ যান এবং প্রয়োজনীয় বিবরণ পূরণ করে একটি নতুন প্রজেক্ট তৈরি করুন।
ধাপ ২: লেআউট ফাইল (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"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="24sp"
android:textStyle="bold" />
<Button
android:id="@+id/button"
android:layout_below="@id/textView"
android:layout_marginTop="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="অ্যাক্টিভিটি রিস্টার্ট করুন" />
</RelativeLayout>
ধাপ ৩: মূল এক্টিভিটি লজিক (src/MainActivity.java)
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
TextView textView;
Button button;
Random random = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = findViewById(R.id.textView);
button = findViewById(R.id.button);
textView.setText("র্যান্ডম নম্বর: " + random.nextInt(100));
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
});
}
}
বিকল্প পদ্ধতি: recreate() মেথড (API 11+)
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
recreate(); // API 11 (Honeycomb) থেকে উপলব্ধ
}
});
ধাপ ৪: ম্যানিফেস্ট ফাইল (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>
অ্যাপ্লিকেশন চালান
আপনার আসল এন্ড্রয়েড ডিভাইস কম্পিউটারের সাথে সংযুক্ত আছে অনুমান করি। Android Studio-তে আপনার প্রজেক্টের এক্টিভিটি ফাইল খুলুন এবং টুলবারের Run আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস নির্বাচন করুন এবং আপনার ডিভাইসে ডিফল্ট স্ক্রিন দেখুন।