|
![](/images/resource/spacer.gif) |
|
Android การส่งค่าตัวแปรจาก fragmentactivity ไปแต่ละ fragment android |
|
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
ผมว่าแบบนี้น่าจะง่ายกว่านะครับ เก็บตัวแปรไว้แล้ว จะใช้ค่อยเรียก เพราะจะมีกี่ Fragment ก็เรียกใช้ได้ตลอด
www.akexorcist.com/2014/09/android-shared-preferences.html
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2015-03-12 15:45:27 |
By :
guest |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
![](/images/resource/viewboard_01.gif?v=1001) |
|
|
![](/images/resource/viewboard_03.gif?v=1001) |
![](/images/resource/viewboard_04.gif?v=1001) |
|
ตัวอย่าง SharedPreferences ครับ
UserHelper.java
public class UserHelper {
Context context;
SharedPreferences sharedPerfs;
Editor editor;
// Prefs Keys
static String perfsName = "UserHelper";
static int perfsMode = 0;
public UserHelper(Context context) {
this.context = context;
this.sharedPerfs = this.context.getSharedPreferences(perfsName, perfsMode);
this.editor = sharedPerfs.edit();
}
public void createSession(String key) {
editor.putBoolean("isLogin", true);
editor.putString("userID", key);
editor.commit();
}
public void deleteSession() {
editor.clear();
editor.commit();
}
public boolean isLogin() {
return sharedPerfs.getBoolean("isLogin", false);
}
public String getUserID() {
return sharedPerfs.getString("userID", null);
}
}
วิธีใช้
Code (Java)
UserHelper usrHelper = new UserHelper(this);
// Create Session
usrHelper.createSession(ID);
// Delete Session
usrHelper.deleteSession();
// Check Login
if (usrHelper.isLogin()) {
} else {
}
// Get UserID
usrHelper.getUserID()
|
![](/images/resource/blockviewtopic_01.gif?v=1001) |
![](/images/resource/blockviewtopic_02.gif?v=1001) |
![](/images/resource/blockviewtopic_03.gif?v=1001) |
![](/images/resource/blockviewtopic_04.gif?v=1001) |
Date :
2015-03-12 15:58:14 |
By :
mr.win |
|
![](/images/resource/blockviewtopic_06.gif?v=1001) |
![](/images/resource/blockviewtopic_07.gif?v=1001) |
![](/images/resource/blockviewtopic_08.gif?v=1001) |
![](/images/resource/blockviewtopic_09.gif?v=1001) |
|
|
![](/images/resource/viewboard_06.gif?v=1001) |
![](/images/resource/viewboard_07.gif?v=1001) |
|
![](/images/resource/viewboard_08.jpg?v=1001) |
![](/images/resource/viewboard_09.gif?v=1001) |
|
|
|
![](/images/digitalocean-banner.jpg)
|
Load balance : Server 03
|