एंड्रॉइड एक्टिविटी को प्रोग्रामेटिकली रीस्टार्ट करना
यह गाइड एक्टिविटी को रीस्टार्ट करने के विभिन्न तरीकों को दर्शाता है, जिसमें इंटेंट फ्लैग्स और रिक्रिएशन शामिल हैं।
लेआउट (activity_main.xml)
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="24dp">
<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_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:text="एक्टिविटी रीस्टार्ट करें" />
</RelativeLayout>
MainActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private Button button;
private final 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(v -> restartActivity());
}
private void restartActivity() {
// विधि 1: recreate() - API 11+ (सबसे सरल)
recreate();
// विधि 2: इंटेंट के साथ (अधिक नियंत्रण)
// Intent intent = getIntent();
// finish();
// startActivity(intent);
// विधि 3: फ्लैग्स के साथ नया इंटेंट (टास्क क्लियर)
// Intent intent = new Intent(this, MainActivity.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
// startActivity(intent);
// finish();
}
}
रीस्टार्ट विधियों की तुलना
| विधि | API लेवल | व्यवहार | उपयोग का मामला |
|---|---|---|---|
recreate() |
11+ | onDestroy() → onCreate(), इंटेंट बरकरार | सिंपल रीस्टार्ट, कॉन्फिग चेंज अनुकरण |
finish() + startActivity(getIntent()) |
सभी | नया इंस्टेंस, पुराना नष्ट | पूर्ण रीसेट, एक्स्ट्रा बरकरार |
FLAG_ACTIVITY_CLEAR_TOP |
सभी | टास्क में ऊपर की एक्टिविटीज़ क्लियर | लॉगआउट, डीप लिंक हैंडलिंग |
ProcessPhoenix (लाइब्रेरी) |
सभी | पूरी प्रोसेस रीस्टार्ट | थीम/भाषा परिवर्तन, क्रिटिकल एरर |
उन्नत: पूरी ऐप रीस्टार्ट (ProcessPhoenix पैटर्न)
// build.gradle implementation "com.github.edenmanm:ProcessPhoenix:1.1.0" // उपयोग: ProcessPhoenix.triggerRebirth(this);
कॉन्फिगरेशन चेंज हैंडलिंग (रीस्टार्ट के बिना)
यदि लक्ष्य केवल भाषा/थीम बदलना है, तो रीस्टार्ट के बजाय कॉन्फिग अपडेट करें:
// भाषा बदलें बिना रीस्टार्ट (API 17+)
Configuration config = new Configuration();
config.setLocale(new Locale("hi")); // हिंदी
getResources().updateConfiguration(config, getResources().getDisplayMetrics());
// थीम बदलें (रीस्टार्ट आवश्यक)
setTheme(R.style.NewTheme);
recreate();
बेस्ट प्रैक्टिसेज
- सरल रीस्टार्ट के लिए
recreate()पसंद करें - रीस्टार्ट से पहले अनसेव्ड डेटा सेव करें (
onSaveInstanceState) - बैक स्टैक व्यवहार पर विचार करें
- थीम/भाषा परिवर्तन के लिए ProcessPhoenix का उपयोग करें
- अनावश्यक रीस्टार्ट से बचें - व्यू अपडेट या फ्रैगमेंट ट्रांजैक्शन का उपयोग करें
सामान्य परिदृश्य
- भाषा परिवर्तन: recreate() या ProcessPhoenix
- थीम परिवर्तन: recreate() आवश्यक
- लॉगआउट: FLAG_ACTIVITY_CLEAR_TASK + NEW_TASK
- डेटा रिफ्रेश: रीस्टार्ट न करें, केवल एडॉप्टर/व्यू अपडेट करें
- क्रैश रिकवरी: ProcessPhoenix या डिफ़ॉल्ट क्रैश हैंडलिंग