Reputation: 61
I'm developing an android app which I want to check if there's internet connection, if it's not the case display a warning message, and when there's internet connection again load a certain url.
It can be said that both displaying the message and checking there's internet connection work... but no separately.
I mean I have the following code:
if (!checkConnectivity())
{
browser.stopLoading();
LayoutInflater layoutinflater = LayoutInflater.from(app);
final View textEntryView;
textEntryView = layoutinflater.inflate(R.layout.exitdialog, null);
new AlertDialog.Builder(app)
.setView(textEntryView)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit")
.setNegativeButton("CANCEL", null)
.show();
while (!checkConnectivity())
{
}
browser.loadUrl(url);
}
If I don't use from while (!checkConnectivity()) to browser.loadUrl(url); then AlertDialog shows properly.
If I don't use from LayoutInflater layoutinflater = LayoutInflater.from(app); to .show(); then the app loads the web page correctly on browser.
But if I use the whole code it looks like it enters the while (!checkConnectivity()) loop before it does the show as what happens when internet connection is restabilished is that the alert dialog is shown briefly and the the web page is loaded.
I find this pretty weird, as I'm not using, as far as I know, threads that could cause that one could be executed before other one, and this is not the case this thing doesn't fulfill one of the most basic things in programming, I mean if an instruction is before another one, the one that's before should be plenty executed logically (except for threads).
Hope someone has an idea about this and can help me.
Upvotes: 0
Views: 105
Reputation: 152927
Your while
loop blocks the UI thread and the dialog has no chance to get displayed.
Busy-looping to poll for a condition is also a big red flag.
Consider waiting for a connectivity event, and use a cancellable progress dialog instead of an an alert dialog to disable other UI until the event is received.
Upvotes: 1