यह उदाहरण दर्शाता है कि Kotlin में Intent.FLAG_ACTIVITY_REORDER_TO_FRONT फ्लैग का उपयोग करके मौजूदा एक्टिविटी को स्टैक के शीर्ष पर कैसे लाया जाए।
चरण 1 - नया प्रोजेक्ट बनाएं
Android Studio: File → New Project।
चरण 2 - activity_main.xml लेआउट
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#008080"
android:padding="5dp"
android:text="TutorialsPoint"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 3 - MainActivity.kt में फ्लैग के साथ इंटेंट
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "KotlinApp"
val intent = Intent(this, NewActivity::class.java)
// यदि NewActivity पहले से बैक स्टैक में है, तो उसे शीर्ष पर लाएं
intent.flags = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT
startActivity(intent)
}
}
चरण 4 - NewActivity.kt बनाएं
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class NewActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_new)
title = "KotlinApp - NewActivity"
}
}
चरण 5 - activity_new.xml लेआउट
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="4dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"
android:background="#008080"
android:padding="5dp"
android:text="TutorialsPoint"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="New Activity"
android:textAlignment="center"
android:textColor="@android:color/holo_blue_dark"
android:textSize="24sp"
android:textStyle="bold" />
</RelativeLayout>
चरण 6 - AndroidManifest.xml में दोनों एक्टिविटी घोषित करें
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="com.example.reorderfront">
<application ...>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".NewActivity" />
</application>
</manifest>
ऐप चलाएं
- ऐप लॉन्च करें → MainActivity दिखेगा → तुरंत NewActivity शुरू होगी
- बैक दबाएं → MainActivity पर वापस आएं
- फिर से ऐप लॉन्च करें (या MainActivity से NewActivity शुरू करें)
- नई इंस्टेंस बनाने के बजाय, मौजूदा NewActivity स्टैक के शीर्ष पर आ जाएगी
संबंधित फ्लैग्स:
FLAG_ACTIVITY_SINGLE_TOP- यदि एक्टिविटी पहले से शीर्ष पर है तो नई इंस्टेंस न बनाएंFLAG_ACTIVITY_CLEAR_TOP- लक्ष्य एक्टिविटी के ऊपर की सभी एक्टिविटी नष्ट करेंFLAG_ACTIVITY_NEW_TASK- नए टास्क में शुरू करेंFLAG_ACTIVITY_CLEAR_TASK- मौजूदा टास्क साफ़ करें (NEW_TASK के साथ)