在我的应用程序中,用户使用 facebook 登录。当用户在第一个活动中单击按钮时,Facebook 对话框打开用户名和密码。用户在此对话框中提供详细信息。成功登录后,用户移动到第二个活动,用户可以在其中看到他的个人详细信息,例如姓名、电子邮件 ID、位置等。现在,对于从应用程序注销,我已将注销选项放在菜单中。下面是我的代码。
MainActivity.java
public class MainActivity extends Activity {
private static List<String> permissions;
Session.StatusCallback statusCallback = new SessionStatusCallback();
ProgressDialog dialog;
String id, username, email, birthday, location, gender;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button fbButton = (Button) findViewById(R.id.login);
/***** FB Permissions *****/
permissions = new ArrayList<String>();
permissions.add("email");
permissions.add("basic_info");
permissions.add("user_about_me");
permissions.add("user_birthday");
permissions.add("user_location");
permissions.add("user_notes");
permissions.add("user_status");
/***** End FB Permissions *****/
fbButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Check if there is any Active Session, otherwise Open New Session
Session session = Session.getActiveSession();
if(!session.isOpened()) {
session.openForRead(new Session.OpenRequest(MainActivity.this).setCallback(statusCallback).setPermissions(permissions));
} else {
Session.openActiveSession(MainActivity.this, true, statusCallback);
}
}
});
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);
session.addCallback(statusCallback);
if(session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) {
session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback).setPermissions(permissions));
}
}
}
private class SessionStatusCallback implements Session.StatusCallback {
@Override
public void call(Session session, SessionState state, Exception exception) {
//Check if Session is Opened or not
processSessionStatus(session, state, exception);
}
}
public void processSessionStatus(Session session, SessionState state, Exception exception) {
if(session != null && session.isOpened()) {
if(session.getPermissions().contains("email")) {
//Show Progress Dialog
dialog = new ProgressDialog(MainActivity.this);
dialog.setMessage("User is Logged in..");
dialog.show();
Request.executeMeRequestAsync(session, new Request.GraphUserCallback() {
@Override
public void onCompleted(GraphUser user, Response response) {
if (dialog!=null && dialog.isShowing()) {
dialog.dismiss();
}
if(user != null) {
Map<String, Object> responseMap = new HashMap<String, Object>();
GraphObject graphObject = response.getGraphObject();
responseMap = graphObject.asMap();
Log.i("FbLogin", "Response Map KeySet - " + responseMap.keySet());
// TODO : Get Email responseMap.get("email");
//String fb_id = user.getId();
//String email = null;
//String name = (String) responseMap.get("name");
if (responseMap.get("email")!=null) {
email = responseMap.get("email").toString();
id = user.getId();
birthday = user.getBirthday();
username = user.getName();
location = (String) user.getLocation().getProperty("name");
gender = (String) user.getProperty("gender");
Intent in = new Intent(getBaseContext(), Second.class);
Bundle extras = new Bundle();
extras.putString("ID",id);
extras.putString("Email", email);
extras.putString("Birthday", birthday);
extras.putString("Username", username);
extras.putString("Location", location);
extras.putString("Gender", gender);
in.putExtras(extras);
startActivity(in);
MainActivity.this.finish();
}
else {
//Clear all session info & ask user to login again
Session session = Session.getActiveSession();
if(session != null) {
session.closeAndClearTokenInformation();
}
}
}
}
});
} else {
session.requestNewReadPermissions(new Session.NewPermissionsRequest(MainActivity.this, permissions));
}
}
}
/********** Activity Methods **********/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d("FbLogin", "Result Code is - " + resultCode +"");
Session.getActiveSession().onActivityResult(MainActivity.this, requestCode, resultCode, data);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
// TODO Save current session
super.onSaveInstanceState(outState);
Session session = Session.getActiveSession();
Session.saveSession(session, outState);
}
@Override
protected void onStart() {
// TODO Add status callback
super.onStart();
Session.getActiveSession().addCallback(statusCallback);
}
@Override
protected void onStop() {
// TODO Remove callback
super.onStop();
Session.getActiveSession().removeCallback(statusCallback);
}
@Override
public void onBackPressed()
{
MainActivity.this.finish();
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}
}
二.java
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.logout:
Session session = Session.getActiveSession();
if (session.isOpened()) {
session.closeAndClearTokenInformation();
Intent in = new Intent(Second.this, MainActivity.class);
startActivity(in);
super.finish();
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
我的问题是,当我单击注销选项时,我将转到第一个活动,但是当我在第一个活动中再次单击登录按钮时,我的应用程序强制关闭。需要做什么才能正确注销?