Issue
At the very start of my application I have a dialoge appear where the user can select between two options:
The code from where I run the dialoge looks like this:
override fun onStart() {
super.onStart()
popup.show(supportFragmentManager, "example d")
Log.d("TAG", "THIS SHOULD BE RUN AFTER I HAVE CLICKED ONE OF THE DIALOGUE BUTTONS")
checkSharedPreference()
switcher()
}
The problem is that the code doesn't stop by popup.show(supportFragmentManager, "example d")
. It keeps running. I assume that this has to do with dialoge being async, no? How do I solve this? Completionhandler
? Asynctask
? Something else?
UPDATE I tried building the clock in accordance with what @Akshay Paliwal said:
class MainActivity : AppCompatActivity() {
public fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val context = applicationContext
val sharedPreference = context.getSharedPreferences(
"key", Context.MODE_PRIVATE)
val builder = AlertDialog.Builder(applicationContext)
builder.setTitle("Välkommen till Demensvård utan tvång.")
.setMessage("Berätta vem du är.")
.setPositiveButton("Anhörig") { dialog, which ->
val version = "anhorig"
sharedPreference.edit().putString("version", version).apply()
checkSharedPreference()
switcher()
}
.setNegativeButton("Personal") { dialog, which ->
val version = "personal"
sharedPreference.edit().putString("version", version).apply()
checkSharedPreference()
switcher()
}
return builder.create()
}
override fun onStart() {
super.onStart()
//THIS? doesn't work...
onCreateDialog(WHAT BUNDLE???)
//OR THIS?
onCreateDialog(what bundle?).show(supportFragmentManager, "example d")
}
How do I call the `onCreateDialog` function? `onCreateDialog(WHAT BUNDLE???)` what bundle should be passed? `onCreateDialog(what bundle?).show(supportFragmentManager, "example d")` Again what bundle?
Or do I need to have onCreateDialog
in a separate class that extends AppCompatDialogFragment
? In that case do I call main.switcher
from the callback in that class?
Solution
You should use dialog callback for this purpose as below:
- Create a dialog or alert dialog
receive callback on action on buttons
AlertDialog.Builder builder = new AlertDialog.Builder(activity); builder.setMessage("message") .setTitle("Warning"); builder.setPositiveButton("Done", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); // do your go ahead task here like //checkSharedPreference() //switcher() } }); builder.setNegativeButton(R.string.update_linkedin_app_cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { dialogInterface.dismiss(); finish(); // to finish current activity } }); builder.create().show();
Answered By - Akshay Paliwal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.