如何在 android 中从 AsyncTask class 完成 Activity?

How to finish Activity from AsyncTask class in android?

我有一个带有汉堡菜单的 activity。当我点击汉堡菜单注销实体时,它会调用 AsyncTask。在调用 API 之后,如果状态码指示成功,那么我需要调用 loginactivity。到这里它工作正常,但我的问题是当我在登录时按下后退按钮然后它返回到上一个屏幕。如何解决这个问题?如何在 AsyncTask 中完成 activity?

public class LandingActivityNew extends AppCompatActivity {
   mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem) {
            drawerLayout.closeDrawers();
            int id = menuItem.getItemId();
            switch (id){
                case R.id.navItemSettings:
                    Intent iSettings = new Intent(LandingActivityNew.this,SettingsActivity.class);
                    startActivity(iSettings);
                    finish();
                    break;
                case R.id.navItemTC:
                   // Toast.makeText(LandingActivityNew.this, "Settings", Toast.LENGTH_LONG).show();
                    Intent iTerms = new Intent(LandingActivityNew.this,TermsConditions.class);
                    startActivity(iTerms);
                    break;
                case R.id.navItemPrivacy:
                    //Toast.makeText(LandingActivityNew.this, "Privacy", Toast.LENGTH_LONG).show();
                    Intent iPrivacy = new Intent(LandingActivityNew.this,ConsentActivity.class);
                    startActivity(iPrivacy);
                    break;
                case R.id.navItemReportProblem:
                    Toast.makeText(LandingActivityNew.this, "Report a Problem", Toast.LENGTH_LONG).show();
                    break;
                case R.id.navItemFAQ:
                    Toast.makeText(LandingActivityNew.this, "FAQ", Toast.LENGTH_LONG).show();
                    break;
                case R.id.navItemShare:
                    Toast.makeText(LandingActivityNew.this, "Share", Toast.LENGTH_LONG).show();
                    break;
                case R.id.navItemLogout:
                    new LogoutAsyncTask(LandingActivityNew.this).execute();
                    break;

            }
            return false;
        }

    });

}

AsyncTask class 这样...

public class LogoutAsyncTask extends AsyncTask<String,String,String> {
   Context context;  
   public LogoutAsyncTask(Context context){
    this.context = context;
    }
   @Override
protected void onPreExecute() {}
@Override
protected String doInBackground(String... params) {}
   @Override
protected void onPostExecute(String file_url) {
    if (pDialog.isShowing()) {
        pDialog.dismiss();
    }
    int appStatusCode = getLogoutResponse.getAppStatusCode();
    if (appStatusCode == Constants.APP_STATUS_CODE_SUCCESS) {
        ***Intent logout=new Intent(context,LoginActivity.class);
        context.startActivity(logout);
        ((Activity)context).finish();***
    } else {
        ExceptionMessages.showAlertDialog(context, "Unable to Logout", getLogoutResponse.getMessages().get(0), true);
    }
}
 }

在 onPost() 中我需要完成 activity 调用,该怎么做?

试试这个方法,

Intent iobj = new Intent(Activity_First.this, Activity_Second.class);
iobj.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(iobj);

How to finish Activity from AsyncTask class in android?

因为在 LogoutAsyncTask class 中传递了 LandingActivityNew.this。这样做:

((LandingActivityNew)context).finish();

onPostExecute方法中

Intent logout=new Intent(yourActivity.this,LoginActivity.class);
        yourActivity.this.startActivity(logout);
        yourActivity.this.finish();

希望它有效。谢谢。如果这不起作用告诉我,我会给你更好的答案

使用以下代码更改 LogoutAsyncTask:

public class LogoutAsyncTask extends AsyncTask<String, String, String> {
        Activity activity;
        LandingActivityNew obj;

        public LogoutAsyncTask(Activity activity) {
            this.activity = activity;
            obj = activity.getApplicationContext();
        }

        @Override
        protected void onPreExecute() {
        }

        @Override
        protected String doInBackground(String... params) {
        }

        @Override
        protected void onPostExecute(String file_url) {
            if (pDialog.isShowing()) {
                pDialog.dismiss();
            }
            int appStatusCode = getLogoutResponse.getAppStatusCode();
            if (appStatusCode == Constants.APP_STATUS_CODE_SUCCESS) {
                ***Intent logout=new Intent(activity,LoginActivity.class);
                activity.startActivity(logout);
                obj.finish();
            } else {
                ExceptionMessages.showAlertDialog(activity, "Unable to Logout", getLogoutResponse.getMessages().get(0), true);
            }
        }
    }