for*_*ill 2 authentication android back-stack
我使用AccountManager在我的应用程序中验证用户
我知道我可以调用getAuthTokenByFeatures,如果没有为我的特定accountType设置帐户,它会启动我的LoginActivity,这正是我想要的,
我有一个BaseActivity,它在onCreate方法上执行,但是在启动LoginActivity时,旧活动仍然在堆栈上,因此通过按后退按钮,用户可以返回到上一个活动是我不想要的行为,我在BaseActibity.onCreate上的代码如下
AccountManager manager = AccountManager.get(getBaseContext());
manager.getAuthTokenByFeatures(
AccountGeneral.ACCOUNT_TYPE,
AccountGeneral.AUTHTOKEN_TYPE_FULL_ACCESS,
null,
this,
null,
null,
new AccountManagerCallback<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> future) {
Bundle bnd = null;
try {
bnd = future.getResult();
final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
LOGV(TAG, "GetTokenForAccount Bundle is " + bnd);
} catch (Exception e) {
LOGE(TAG, "exception while getAuthTokenByFeatures", e);
}
}
}
, null);
Run Code Online (Sandbox Code Playgroud)
问题是:如何禁用该返回行为?如果是我以编程方式调用LoginActivity,我只需在BaseActivity上调用finish()
小智 8
在您的AccountAuthenticatorActivity上,您可以覆盖后退按钮行为:
@Override
public void onBackPressed() {
Intent result = new Intent();
Bundle b = new Bundle();
result.putExtras(b);
setAccountAuthenticatorResult(null); // null means the user cancelled the authorization processs
setResult(RESULT_OK, result);
finish();
}
Run Code Online (Sandbox Code Playgroud)
现在您可以对此取消做出反应.在你的代码中:
@Override
public void run(AccountManagerFuture<Bundle> future) {
Bundle bnd = null;
try {
if (future.isCancelled()) {
// Do whatever you want. I understand that you want to close this activity,
// so supposing that mActivity is your activity:
mActivity.finish();
return;
}
bnd = future.getResult();
final String authtoken = bnd.getString(AccountManager.KEY_AUTHTOKEN);
LOGV(TAG, "GetTokenForAccount Bundle is " + bnd);
} catch (Exception e) {
LOGE(TAG, "exception while getAuthTokenByFeatures", e);
}
}
Run Code Online (Sandbox Code Playgroud)