I am trying to show a messagebox with OK button. I am using AlertDialog for this purpose and I realised that it is not blocking the code. Example:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new AlertDialog.Builder(this).setTitle("Test dlg").setMessage("Alert 1")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
})
.setNegativeButton("", null)
.show();
new AlertDialog.Builder(this).setTitle("Test dlg").setMessage("Alert 2")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
})
.setNegativeButton("", null)
.show();
//...continue with UI initialization here...
}
When I start activity, it shows Alert2, When I press ok it shows Alert1 afterwards.
I need to have blocking code dialog, so at first it should show Alert1 message, wait until user presses OK button then continue to execute the code and show Alert2 message, etc.. Example:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
msgBox("Test dlg", "Alert 1");
msgBox("Test dlg", "Alert 2");
//...continue with UI initialization here...
}
private void msgBox(String title, String msg){
//?????
/* WRONG, NON-BLOCKING
new AlertDialog.Builder(this).setTitle(title).setMessage(msg)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
})
.setNegativeButton("", null)
.show();
*/
}
What should I write on //????? place in msgBox method?
MORE EXAMPLE, I need something like this:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (isInitialisationDataFailed()){
msgBox("Alert", "Cannot open activity, sorry");
myActivity.this.finish();
return;
}
}
But this does not work. Activity finish runs quicker than alert appear on the screen.
The main idea to have messagebox code separate to own method to make it reusable. How to achieve this?
///////////////////////////////// another example:
private void init(){
//init code here...
if (isSomethingWhrong()){
msgbox("wrong stuff will be fixed");
//do fix wrong stuff here...
}
if (isAnotherthingWrong()){
msgbox("more wrong stuff will be fixed");
//do fix more wrong stuff....
}
//continue init code here...
}
private void msgbox(String msg){
//BLOCKING DIALOG REALISATION here...
}
and as alternative this:
private void init(){
//init code here...
handleWrongStuff();
}
private void handleWrongStuff(){
if (isSomethingWhrong()){
new AlertDialog.Builder(activity)
.setTitle("Test")
.setMessage("wrong stuff will be fixed")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do fix wrong stuff here...
handleMoreWrongStuff();
}
})
.setNegativeButton("", null)
.show();
}
else{
handleMoreWrongStuff();
}
}
private void handleMoreWrongStuff(){
if (isAnotherthingWrong()){
new AlertDialog.Builder(activity)
.setTitle("Test")
.setMessage("more wrong stuff will be fixed")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//do fix more wrong stuff here...
continueInit();
}
})
.setNegativeButton("", null)
.show();
}
else{
continueInit();
}
}
private void continueInit(){
//continue init code here...
}
do you see the difference in complexity? In order to make init code working in Android I need to split it to separate methods but not on logical blocks but when I need to show dialogs. Moreover the code for initialization dialogs are repeated and became ugly and unreadable.
Put the code to show 2nd dialog in onClick of Positive button of First alert dialog.
using this way you can't stop the execution but instead of this you can put on button action listener so when button pressed at that time listener will be invoked and you can execute the code for e.g. in your case when you press the ok button then show the next alert from listener.
new AlertDialog.Builder(this).setTitle("Test dlg").setMessage("Alert 1")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
new AlertDialog.Builder(YourActivity.this).setTitle("Test dlg").setMessage("Alert 2")
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {}
})
.setNegativeButton("", null)
.show();
}
})
.setNegativeButton("", null)
.show();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With