1
如何使此代碼記住用戶在Android應用程序中所做的登錄。我需要的應用程序來記住一些數據的喜歡的用戶名和個人資料照片,但現在的應用程序,當我重新啓動(關閉並重新開啓),丟失數據的喜歡的用戶名和個人資料圖片應用使應用程序在退出應用程序後記住Facebook登錄會話
public class SelfTrail extends AppCompatActivity {
private LoginButton btnLogin;
private TextView facebookName;
private CallbackManager callbackManager;
private ProfilePictureView profilePictureView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_self_trail);
btnLogin = (LoginButton)findViewById(R.id.login_button);
facebookName = (TextView)findViewById(R.id.name);
profilePictureView = (ProfilePictureView)findViewById(R.id.image);
btnLogin.setReadPermissions(Arrays.asList("public_profile, email"));
callbackManager = CallbackManager.Factory.create();
btnLogin.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
@Override
public void onSuccess(LoginResult loginResult) {
GraphRequest request = GraphRequest.newMeRequest(
loginResult.getAccessToken(),
new GraphRequest.GraphJSONObjectCallback() {
@Override
public void onCompleted(JSONObject object, GraphResponse response) {
Log.v("Main", response.toString());
setProfileToView(object);
}
});
Bundle parameters = new Bundle();
parameters.putString("fields", "id,name");
request.setParameters(parameters);
request.executeAsync();
}
@Override
public void onCancel() {
}
@Override
public void onError(FacebookException exception) {
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}
private void setProfileToView(JSONObject jsonObject) {
try {
facebookName.setText(jsonObject.getString("name"));
profilePictureView.setPresetSize(ProfilePictureView.NORMAL);
profilePictureView.setProfileId(jsonObject.getString("id"));
} catch (JSONException e) {
e.printStackTrace();
}
}}
隨時糾正代碼,謝謝
您是否嘗試使用偏好設置,或者您也可以使用onSaveInstanceState。只要看看它是否適合你。 –
@AmitKumar我是那麼沮喪的人我不在哪裏,如何實施,一直在嘗試,因爲你說 – Eggsy
http://stackoverflow.com/questions/6525698/how-to-use-onsavedinstancestate-example-please - 你試試有些像onsaveinstancestate的答案中的這樣? –