I know this question has been asked many times but hear me out. I have MainActivity which is a LoginActivity & it uses facebook login and AccountKit From Facebook to login and start next activity which is a ContactsActivity. When i am in ContactsActivity and press back button it takes me to MainActivity but i want to close the application as user has successfully logined in and if the user again opens the app it should open ContactsActivity directly as user has signed in successfully previously.
Problem is when i press Back button in Contacts Activity,it takes me to LoginActivity,i already tried finish() after startActivity(intent) which closes my app and not takes me to ContactsActivity after successfull signin.If i open my app again then it takes me directly to ContactsActivity. I even tried onBackPressed in which i wrote finish but it doesnt work. In Android Manifest also i wrote android:noHistory="true" in MainActivity tag but it doesn't work. Please help
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();
}
}
}
}