Android में मौजूदा एक्टिविटी को फोरग्राउंड में लाना
यह उदाहरण दर्शाता है कि Intent.FLAG_ACTIVITY_REORDER_TO_FRONT फ्लैग का उपयोग करके पहले से चल रही एक्टिविटी को टास्क स्टैक के शीर्ष पर कैसे लाया जाता है, बजाय नया इंस्टेंस बनाने के।
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें और प्रोजेक्ट बनाएं।
चरण 2: Main एक्टिविटी लेआउट
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>
चरण 3: MainActivity.java - फ्लैग के साथ इंटेंट
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);
}
}
चरण 4: 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_activity);
}
}
चरण 5: activity_my_activity.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>
चरण 6: 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>
महत्वपूर्ण Intent फ्लैग्स
| फ्लैग | व्यवहार |
|---|---|
| FLAG_ACTIVITY_REORDER_TO_FRONT | मौजूदा इंस्टेंस को सामने लाता है, नया नहीं बनाता |
| FLAG_ACTIVITY_SINGLE_TOP | यदि पहले से टॉप पर है तो नया इंस्टेंस नहीं बनाता |
| FLAG_ACTIVITY_CLEAR_TOP | स्टैक में ऊपर की सभी एक्टिविटी खत्म करता है |
| FLAG_ACTIVITY_NEW_TASK | नए टास्क में शुरू करता है |
| FLAG_ACTIVITY_CLEAR_TASK | टास्क की सभी एक्टिविटी खत्म करता है |