简体   繁体   中英

Starting Application Again from MAIN & Clearing the activity stack

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...

protected void CreateNetErrorDialog(String errorMessage) {
    Log.d(TAG, "CreateNetErrorDialog() entered...");

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage(errorMessage)
    .setTitle("Unable to connect")
    .setCancelable(false)
    .setPositiveButton("Settings",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                Intent i = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
                startActivity(i);
            }
        }
    )
    .setNegativeButton("Cancel",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                MyMainActivity.this.finish();
            }
        }
    );
    AlertDialog alert = builder.create();
    alert.show();
    Log.d(TAG, "Exiting CreateNetErrorDialog()...");
}

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...

private class FileDownloader extends AsyncTask<String, Void, Void> {
    private Boolean Success = false;
    private String ResultString = "";

    @Override
    protected Void doInBackground(String... params) {
        try {
            // Do whatever
        }
        catch (Exception e) { // <-- Use the correct exception type
              ResultString = "Some error message";
        }
    }

    @Override
    protected void onPostExecute(Void result) {
        if (!Success)
            CreateNetErrorDialog (ResultString);
        else
            // Do whatever
    }
}

I don't know exactly what you want but try something like this instead of making a connection and writing code in catch block which is not a good practice

to check for network use following code

ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo ni = cm.getActiveNetworkInfo();

and then depending on result of above code

if (ni == null)
{
      //Show you network error activity
}
else
{
    //Show some other activity
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM