我知道这个问题已被多次询问但是听我说。我有MainActivity这是一个LoginActivity&它使用Facebook登录和AccountKit从Facebook登录并开始下一个活动,这是一个ContactsActivity。当我在ContactsActivity并按回按钮时,它将我带到MainActivity,但我想在用户成功登录时关闭应用程序,如果用户再次打开应用程序,它应该直接打开ContactsActivity,因为用户先前已成功登录。
问题是当我按下“联系人活动”中的“返回”按钮时,它会将我带到LoginActivity,我已经尝试在startActivity(intent)之后尝试完成()关闭我的应用程序,并且在成功登录后不会将我带到ContactsActivity。如果我打开我的应用程序再次,它直接带我到ContactsActivity。 我甚至试过onBackPressed,其中我写完了但它不起作用。 在Android Manifest中我也写了android:noHistory =" true"在MainActivity标签中,但它不起作用。 请帮忙
activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="pritish.sawant.com.simplychat.MainActivity">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/loginwithphonenumber"
android:text="@string/loginwithphoneno"
style="@style/Base.Widget.AppCompat.Button.Borderless"
android:textColor="@android:color/white"
android:background="@drawable/login_button_selector"
android:textSize="16sp"
android:layout_centerInParent="true"
android:textAlignment="center"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/email_login_button"
android:text="@string/loginwithemail"
android:layout_below="@+id/loginwithphonenumber"
style="@style/Base.Widget.AppCompat.Button.Borderless"
android:textColor="@android:color/white"
android:background="@drawable/login_button_selector"
android:textSize="16sp"
android:textAlignment="center"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="8dp"
/>
<com.facebook.login.widget.LoginButton
android:id="@+id/facebook_login_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/email_login_button"
android:textSize="16sp"
android:textAllCaps="true"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
android:paddingTop="14dp"
android:paddingBottom="14dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
/>
</RelativeLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
public static int APP_REQUEST_CODE=1;
private LoginButton facebookloginButton;
private Button emailloginbutton,phonenologinbutton;
private CallbackManager callbackManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
facebookloginButton=(LoginButton)findViewById(R.id.facebook_login_button);
emailloginbutton=(Button)findViewById(R.id.email_login_button);
phonenologinbutton=(Button)findViewById(R.id.loginwithphonenumber);
callbackManager=CallbackManager.Factory.create();
facebookloginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
launchContactsActivity();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException error) {
Toast.makeText(getApplicationContext(), R.string.loginfailed,Toast.LENGTH_LONG).show();
}
});
AccessToken accessToken= AccountKit.getCurrentAccessToken();
com.facebook.AccessToken loginToken= com.facebook.AccessToken.getCurrentAccessToken();
if(accessToken!=null || loginToken!=null){
launchContactsActivity();
}
emailloginbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onLogin(LoginType.EMAIL);
}
});
phonenologinbutton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onLogin(LoginType.PHONE);
}
});
}
private void launchContactsActivity(){
Intent intent=new Intent(getApplicationContext(),ContactsActivity.class);
startActivity(intent);
finish();
}
private void onLogin(final LoginType loginType) {
// create intent for the Account Kit activity
final Intent intent = new Intent(this, AccountKitActivity.class);
// configure login type and response type
AccountKitConfiguration.AccountKitConfigurationBuilder configurationBuilder =
new AccountKitConfiguration.AccountKitConfigurationBuilder(
loginType,
AccountKitActivity.ResponseType.TOKEN
);
final AccountKitConfiguration configuration = configurationBuilder.build();
// launch the Account Kit activity
intent.putExtra(AccountKitActivity.ACCOUNT_KIT_ACTIVITY_CONFIGURATION, configuration);
startActivityForResult(intent, APP_REQUEST_CODE);
finish();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode,resultCode,data);
if(requestCode==APP_REQUEST_CODE){
AccountKitLoginResult accountKitLoginResult=data.getParcelableExtra(AccountKitLoginResult.RESULT_KEY);
if(accountKitLoginResult.getError()!=null){
String toastMessage = accountKitLoginResult.getError().getErrorType().getMessage();
Toast.makeText(this, toastMessage, Toast.LENGTH_LONG).show();
} else if (accountKitLoginResult.getAccessToken() != null) {
launchContactsActivity();
}
}
}
}
答案 0 :(得分:0)
通常情况下,您不应该关闭应用程序 - 用户将按回原点或通过按回来关闭您的活动将其移回。
但你可以使用这个提示:
1)如果不需要在后台堆叠中,则在启动新活动后关闭旧活动:
startActivity(newActivityIntent);
finish();
覆盖onBackPressed,应用程序将关闭
2)如果您需要http://developer.android.com/reference/android/app/Activity.html#moveTaskToBack(boolean)
,可以将任务移回(而不是关闭)