简体   繁体   中英

Login with Facebook SDK 4 in Android application

I'm trying to do a simple application in which I can login to by using Facebook's login button. The way I want it to work is that after success in login new activity starts(that I manage to do)The Problem is that when I close and reopen my app the first screen I'm getting is an activity with "Log out" -button so that I have to log out and then login again to get my new activity. can any One please help me with that:S? here is the Code:

public class MainActivity extends AppCompatActivity {

private LoginButton loginButton;
private CallbackManager callbackManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    FacebookSdk.sdkInitialize(getApplicationContext());

    callbackManager = CallbackManager.Factory.create();
    setContentView(R.layout.activity_main);

    loginButton = (LoginButton) findViewById(R.id.loginBtn);

    loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
        @Override
        public void onSuccess(LoginResult loginResult) {
            Intent i = new Intent(MainActivity.this, MainWindow.class);
            startActivity(i);
        }

        @Override
        public void onCancel() {
        }

        @Override
        public void onError(FacebookException e) {

        }
    });
}

public View onCreateView(String name, Context context, AttributeSet attrs) {
    return super.onCreateView(name, context, attrs);
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    callbackManager.onActivityResult(requestCode, resultCode, data);
}

You can achieve your requirement with shared preferences. So for that you can follow this link for easy and better understanding.

For your requirement put one boolean element in onSuccess() Method with shared preference and check this variable in Starting of application for your preferable action.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM