简体   繁体   English

在Android应用程序中使用Facebook SDK 4登录

[英]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. 我正在尝试做一个简单的应用程序,可以在其中使用Facebook的登录按钮登录。 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. 根据您的要求,在具有共享首选项的onSuccess()方法中放置一个布尔元素,并在“启动应用程序”中检查此变量以采取您希望的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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