এই উদাহরণটি দেখায় কিভাবে অ্যান্ড্রয়েডে একটি এক্টিভিটি ফোরগ্রাউন্ডে (স্ট্যাকের টপে) আনা যায়।
ধাপ ১: নতুন প্রজেক্ট তৈরি করুন
Android Studio-তে File → New Project এ যান এবং প্রজেক্ট তৈরি করার জন্য সকল প্রয়োজনীয় তথ্য পূরণ করুন।
ধাপ ২: লেআউট XML ফাইল যোগ করুন
res/layout/activity_main.xml ফাইলে নিচের কোডটি যোগ করুন।
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
ধাপ ৩: MainActivity.java ফাইলে কোড যোগ করুন
src/MainActivity.java ফাইলে নিচের কোডটি যোগ করুন। Intent.FLAG_ACTIVITY_REORDER_TO_FRONT ফ্ল্যাগ ব্যবহার করে এক্টিভিটি ফোরগ্রাউন্ডে আনা হয়েছে।
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent i = new Intent(this, MyActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
}
}
ধাপ ৪: MyActivity.java ক্লাস তৈরি করুন
নতুন একটি এক্টিভিটি MyActivity.java তৈরি করে নিচের কোডটি যোগ করুন।
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class MyActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_acitivity);
}
}
ধাপ ৫: activity_myacitivity.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=".MyActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MYACTIVITY"
android:layout_centerInParent="true"
android:textAlignment="center"
android:textSize="24sp"
android:textStyle="bold"/>
</RelativeLayout>
ধাপ ৬: 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=".MyActivity">
<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 আইকনে ক্লিক করুন। আপনার মোবাইল ডিভাইস সিলেক্ট করুন এবং ডিভাইসে ডিফল্ট স্ক্রিন দেখুন।