I have developed an android 2.1 application which consumes Soap Web Service.
When my application gets started, it first checks whether there is internet connectivity or not.
If so, it will display corresponding activity.
If not then it will display an Activity(NetworkErrorActivity) giving information about network errors and all.
The problem is if there is no internet connectivity, it shows me the NetworkErrorActivity.
Now when user presses back button it redirects the user to Home.
I have overriden onBackPressed method like this :
@Override
public void onBackPressed() {
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
return;
}
After pressing back button, it shows me the android home screen.
But the problem is when i start the same application again, it shows me the NetworkErrorActivity even if there is network connectivity. Could not start the application from Main Launcher Activity. It always shows me the same Activity again and again.
I use an AlertDialog (not an Activity) to inform the user about needing a network connection with an option to take them to network settings (to enable mobile/wi-fi) connections. If they choose to click ‘Cancel’ instead I force the main activity to finish().
Check for network connection in main activity onResume() and if there’s no connection then call CreateNetErrorDialog()…it isn’t possible to use BACK to dismiss the dialog – only Cancel which kills the main activity.
Example…
EDIT: Call the CreateNetErrorDialog from the onPostExecute() method of your AsyncTask – it runs on the UI thread (doInBackground doesn’t). Example from my own code…