Search code examples
androidandroid-asynctaskactivity-finish

How to finish Activity from AsyncTask class in android?


I have an activity with a hamburger menu. When I click on the hamburger menu logout enty, it will call an AsyncTask. After the API was called, if the statuscode indicates success then I need to call loginactivity. Up to here it works fine, but my problem is when I press the back button in login then it goes back to the previous screen. How to fix this? How do I finish activity in AsyncTask?

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

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);
    }
}
 }

in onPost() I need to finish the activity call, how to do that?


Solution

  • Try this way,

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