简体   繁体   中英

activity.finish() is not stopped current activity

I have created an android apps that using async task to call web service, when authentication fail, user will stop current activity and redirect back to login page.

My problem is when user redirect back to login page, a toast text still showing, this toast text is under async thread onPostExceute() event.

any solution for this problem?

in the webService.cs

catch (UnauthorizedException ua) {
        Log.d(tag, ua.getMessage());
        Intent intent = new Intent(activity, LoginActivity.class);
        if(condition a){
            intent.putExtra("toast_text", R.string.a);
        }else{
            intent.putExtra("toast_text", R.string.b);
        }
        activity.finish();
        activity.startActivity(intent);
}
return null;

then in asyncTask.cs

protected JSONArray doInBackground(String... parameters) {     
        ConnectivityManager cm =
                (ConnectivityManager) SingleFormActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);

        NetworkInfo activeNetwork = cm.getActiveNetworkInfo();
        boolean isConnected = activeNetwork != null &&
                activeNetwork.isConnectedOrConnecting();

        if (isConnected) {
        }

     return null;
}

public void onPostExecute(JSonArray result){
     if(result != null){
     }else{
          Toast.makeText(Something.this, R.string.b, Toast.LENGTH_LONG).show();
     }
}

you can Use

Create Toast like

 Toast toast = Toast.makeText(getApplicationContext(), "", Toast.LENGTH_LONG);

and before calling finish() cancel toast using below code

 if (toast != null )
      toast.cancel();

In onPause() add below code

if(isFinishing()){
    if (toast != null || toast.getView().getWindowVisibility() == View.VISIBLE) {
        toast.cancel();
    }
}

Try like this Return request status from doInBackground.and show toast using that status in onPostExecute may be there is any syntax error in this code.But i hope it help you

public class AsyncConnectTask extends AsyncTask<Void, Void, Boolean> {

private MyInterface mListener;


public AsyncConnectTask(Context context, String address, String user,
    String pass, int port, MyInterface mListener) {
               }

@Override
protected Boolean doInBackground(Void... params) {
    ....
    return result;
}


@Override
protected void onPostExecute(Boolean result) {
    if (result == true) {
        //Show toast here
}else{

 }
}
}

when authentication fail, user will stop current activity and redirect back to login page.

with your above question, I assume you got two different activity (one for login and another for doing something also showing the toast message. If its correct then before showing the toast check if the activity is finishing if not then show the toast.

if(!isFinishing()){

 // show toast
}

http://developer.android.com/reference/android/app/Activity.html#isFinishing%28%29

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