我有汉堡菜单的活动。当我点击汉堡菜单退出时,它会调用AsyncTask
。调用API后,如果状态代码显示成功,那么我需要调用loginactivity。到目前为止它工作正常,但我的问题是当我在登录时按下后退按钮然后它回到上一个屏幕。如何解决这个问题?如何在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类......
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()中我需要完成活动调用,怎么做?
答案 0 :(得分:2)
试试这种方式,
Intent iobj = new Intent(Activity_First.this, Activity_Second.class);
iobj.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(iobj);
答案 1 :(得分:1)
如何从android中的AsyncTask类完成Activity?
因为在LandingActivityNew.this
课程中传递了LogoutAsyncTask
。这样做:
((LandingActivityNew)context).finish();
onPostExecute
方法中的
答案 2 :(得分:0)
Intent logout=new Intent(yourActivity.this,LoginActivity.class);
yourActivity.this.startActivity(logout);
yourActivity.this.finish();
希望它有效。感谢名单。如果这不起作用告诉我,我给你更好的答案
答案 3 :(得分:0)
使用以下代码更改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);
}
}
}