I am trying to show a message with an OK button. I am using AlertDialog for this purpose, and I realized that it does not block 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();
When I run the action, it shows Alert2. When I click ok, then Alert1 appears.
I need to have a dialog box with the lock code, so first the message Alert1 should appear, wait for the user to click OK, and then continue to execute the code and display the message Alert2, etc. Example:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); msgBox("Test dlg", "Alert 1"); msgBox("Test dlg", "Alert 2");
What should I write in // ????? 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 that does not work. The task completes faster than a warning appears on the screen.
The basic idea is to have messaging code separate from your own method to make it reusable. How to achieve this?
///////////////////////////////// another example:
private void init(){
and alternatively 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? To make the init code work on Android, I need to split it into separate methods, but not into logical blocks, but when I need to show dialogs. In addition, the code for initialization dialogs repeats and becomes ugly and unreadable.