这就是我推送数据的方式,它包括我考虑该sharedpref项是否为null,如果它为null,则调用生成用户名的方法,并重新启动活动,以便看到更改。
SharedPreferences prefs = getApplication().getSharedPreferences("SophiePrefs", 0);
mUsername = prefs.getString("username", null);
if(mUsername!=null){
mFirebaseBackupHistory.child(mUsername).push().setValue(allthemsgs);
}else {
setupUsername();
finish();
overridePendingTransition(0, 0);
startActivity(getIntent());
overridePendingTransition(0, 0);
}
这就是我获取用户名的方式:
SharedPreferences prefs = getApplication().getSharedPreferences("ChatPrefs", 0);
mUsername = prefs.getString("username", null);
if (mUsername == null) {
Random r = new Random(); // Assign a random user name if we don't have one saved.
mUsername = "Client" + r.nextInt(100000);
prefs.edit().putString("username", mUsername).commit();
}
我继续得到这个
{
"tutored" : {
"-KSRR7nDjf01iyexk_wl" : {
"left" : false,
"text" : "hi"
},
"-KSRR8LPWohiIfA0BnCp" : {
"left" : true,
"text" : "Hey,I'm Sophie,What would you like to know"
},
"-KSRR9xHoEf1g-Jf8BHN" : {
"left" : false,
"text" : "shit"
},
"-KSRRACSJXn8db6JhGDE" : {
"left" : true,
"text" : "Wait a moment as I look for the answer"
},
"-KSRRBuFcwTJpjIh9qum" : {
"left" : false,
"text" : "this sucks"
},
"-KSRRBy2gFcY2U7iLaXv" : {
"left" : true,
"text" : "Wait a moment as I look for the answer"
}
}
}
没有mUsername没有辅导......它是我为每个人用应用生成的动态字符串