सिंगलटन पैटर्न और अलर्ट डायलॉग
सिंगलटन डिज़ाइन पैटर्न किसी क्लास के इंस्टेंशिएशन को एक इंस्टेंस तक सीमित करता है। इस उदाहरण में हम सिंगलटन क्लास से अलर्ट डायलॉग दिखाएंगे।
उदाहरण: सिंगलटन अलर्ट डायलॉग
चरण 1: नया प्रोजेक्ट बनाएं
Android Studio में File → New Project चुनें।
चरण 2: लेआउट फ़ाइल (res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<Button
android:id="@+id/show"
android:text="सिंगलटन से अलर्ट डायलॉग"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
चरण 3: MainActivity.java
package com.example.singletonalertdialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button show;
SingletonExample singletonExample;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
show = findViewById(R.id.show);
singletonExample = SingletonExample.getInstance();
singletonExample.init(getApplicationContext());
show.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
singletonExample.showAlertDialog(MainActivity.this);
}
});
}
}
चरण 4: सिंगलटन क्लास (SingletonExample.java)
package com.example.singletonalertdialog;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.widget.Toast;
public class SingletonExample {
private static SingletonExample ourInstance = new SingletonExample();
private Context appContext;
private SingletonExample() { }
public static Context get() {
return getInstance().getContext();
}
public static synchronized SingletonExample getInstance() {
return ourInstance;
}
public void init(Context context) {
if (appContext == null) {
this.appContext = context;
}
}
private Context getContext() {
return appContext;
}
public void showAlertDialog(final MainActivity mainActivity) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(mainActivity);
alertDialogBuilder.setMessage("क्या आप निश्चित हैं? आप निर्णय लेना चाहते हैं");
alertDialogBuilder.setPositiveButton("हाँ",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(mainActivity, "आपने हाँ बटन क्लिक किया", Toast.LENGTH_LONG).show();
}
});
alertDialogBuilder.setNegativeButton("नहीं", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mainActivity.finish();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
ऐप चलाएं
ऐप लॉन्च करें और "सिंगलटन से अलर्ट डायलॉग" बटन दबाएं। सिंगलटन क्लास से अलर्ट डायलॉग दिखाई देगा जिसमें "हाँ" और "नहीं" विकल्प होंगे।
मुख्य बिंदु
- सिंगलटन पैटर्न अलर्ट डायलॉग लॉजिक को केंद्रीकृत करता है
- ग्लोबल कॉन्टेक्स्ट का उपयोग करके टोस्ट दिखाया जाता है
AlertDialog.Builderसे डायलॉग बनाया जाता है- पॉजिटिव बटन टोस्ट दिखाता है, नेगेटिव बटन एक्टिविटी खत्म करता है
- नोट:
TextToSpeechइंपोर्ट अप्रयुक्त है, इसे हटाया जा सकता है