[英]How to hide the Facebook Login page activity after successful login in android?
I am a newbiew in Android. 我是Android的新手。 I have intergarted
Facebook
in my Android
app. 我在
Android
应用程序中加入了Facebook
。 I have a Login page with a button. 我有一个带有按钮的登录页面。 On click of that button the FB login page appears.After the successful login of Facebook, the user gets to go to the MainActivity.
单击该按钮后,将显示FB登录页面。Facebook成功登录后,用户可以转到MainActivity。 I want to hide my Login page after the successful login of FB.
成功登录FB后,我想隐藏我的登录页面。 That is, when I reload the app, if the user has already logged in then the MainActivity only should be dispalyed and not the Login page.
也就是说,当我重新加载应用程序时,如果用户已经登录,则应该仅显示MainActivity而不是“登录”页面。
My code is as follows: 我的代码如下:
public class Login extends Activity {
private static final String URL_PREFIX_FRIENDS = "https://graph.facebook.com/me/friends?access_token=";
//private TextView textInstructionsOrLink;
private ImageButton buttonLoginLogout;
public static final String PREFS_PRIVATE = "PREFS_PRIVATE";
private Button b;
private Session.StatusCallback statusCallback = new SessionStatusCallback();
Databasehandler db=new Databasehandler(this);
HashMap<String, String>map=new HashMap<String, String>();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
buttonLoginLogout = (ImageButton)findViewById(R.id.imageButton1);
Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS);
Session session = Session.getActiveSession();
if (session == null) {
if (savedInstanceState != null) {
session = Session.restoreSession(this, null, statusCallback, savedInstanceState);
}
if (session == null) {
session = new Session(this);
}
Session.setActiveSession(session);
if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
}
}
updateView();
}
@Override
public void onStart() {
super.onStart();
Session.getActiveSession().addCallback(statusCallback);
}
@Override
public void onStop() {
super.onStop();
Session.getActiveSession().removeCallback(statusCallback);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
Session session = Session.getActiveSession();
Session.saveSession(session, outState);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
private void updateView() {
Session session = Session.getActiveSession();
if (session.isOpened()) {
Request.executeMeRequestAsync(session,new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
// TODO Auto-generated method stub
final String t1=user.getId();
final String t2=user.getName();
// SharedPreferences myPrefs =getSharedPreferences("myprefs",MODE_WORLD_READABLE);
int count=db.getme();
if(count==0)
{
map.put("uid",t1.toString());
map.put("name",t2.toString());
db.insertnewuser(map);
}
}
});
Intent ik=new Intent(Login.this,MainActivity.class);
ik.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(ik);
} else {
buttonLoginLogout.setOnClickListener(new OnClickListener() {
public void onClick(View view) { onClickLogin(); }
});
}
}
private void onClickLogin() {
Session session = Session.getActiveSession();
if (!session.isOpened() && !session.isClosed()) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback));
} else {
Session.openActiveSession(this, true, statusCallback);
}
}
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
updateView();
}
}
}
Just add below code inside updateView
method. 只需在
updateView
方法内添加以下代码updateView
。
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
So your full method will look something like below code. 因此,您的完整方法将类似于以下代码。
private void updateView() {
Session session = Session.getActiveSession();
if (session.isOpened()) {
Request.executeMeRequestAsync(session,new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
// TODO Auto-generated method stub
final String t1=user.getId();
final String t2=user.getName();
// SharedPreferences myPrefs =getSharedPreferences("myprefs",MODE_WORLD_READABLE);
int count=db.getme();
if(count==0)
{
map.put("uid",t1.toString());
map.put("name",t2.toString());
db.insertnewuser(map);
Intent intent = new Intent(Login.this, MainActivity.class);
startActivity(intent);
}
}
So basically it will automatically moves to next screen whenever facebook session is opened. 因此,基本上,只要打开Facebook会话,它将自动移至下一个屏幕。
//After successfull login it hide the facebook button //成功登录后,隐藏facebook按钮
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
//after successful login into the facebook it hide the logout button
loginButton.setVisibility(View.GONE);
}
//if you want to logout after login to facebook successfully then you want to write this line inside the onSccess() method at the end
//after successful login into the facebook it hide the logout button
LoginManager.getInstance().logOut();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.