[英]App does not save data before activity closes, or not loading data after activity is created - SharedPreferences
主要活動數據無法使用SharedPreferences正確保存/加載
請參閱上面的鏈接,以了解其他人的原始問題和反饋...概括起來,SharedPreferences不是保存或沒有正確加載數據...請參閱我的應用程序圖片,它將更好地顯示問題:
至於主要活動背后的代碼,我嘗試了不同人提出的不同建議,並且感謝任何曾經或願意提供幫助的人:)這時,這是與MainActivity中的問題相關的代碼:(請注意:為了節省空間,我只顯示一個Bundle(getFromQuizOne),但是對於所有10個測驗,我都具有相同的Bundle / Bundle代碼。此外,我只放入了一個測驗按鈕setOnClickListener,但是適用相同的代碼用於所有其他測驗按鈕)。
//Get points from Quiz One
Bundle getFromQuizOne = getIntent().getExtras();
if(getFromQuizOne != null)
{
//Get points from Quiz One
numberQuizOnePoints = getFromQuizOne.getInt("PointsFromAllQuizOne");
SharedPreferences shareInt = getApplicationContext().getSharedPreferences(QUIZ, MODE_PRIVATE);
SharedPreferences.Editor editInt = shareInt.edit();
//Save the value of points from Quiz One with SharedPreferences
editInt.putInt("quizOnePointsYeah", numberQuizOnePoints);
editInt.commit();
//Call arrayMethod, which uses for loop to get and display value of points for each quiz
arrayMethod();
}//end if
//My actual project has same kind of Bundle for all other quizzes, but to save space, i am just listing Bundle for quiz 1
//Directs user to Quiz One, first screen
quizOneButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
//Subtract out any points that user already got for Quiz 1 from total...
//In case user wants to or has to re-take Quiz 1
totalPoints = totalPoints - numberQuizOnePoints;
//Subtract out any points that user already got for Quiz 1 from itself, equal 0
numberQuizOnePoints -= numberQuizOnePoints;
//Go to the Quiz One, first screen
Intent directToQuizOnePartOne = new Intent();
directToQuizOnePartOne.setClass(getBaseContext(), QuizOnePartOne.class);
startActivity(directToQuizOnePartOne);
finish();
}//end void onClick quizOne
});//end setOnClickListener quizOne
//My project has button click listeners for all 10 quizzes, that look similar to quizOneButton onClick action listener, but to save on space, I didn't paste it here
public void arrayMethod()
{
int[] points = {numberQuizOnePoints, numberQuizTwoPoints, numberQuizThreePoints,
numberQuizFourPoints, numberQuizFivePoints, numberQuizSixPoints, numberQuizSevenPoints,
numberQuizEightPoints, numberQuizNinePoints, numberQuizTenPoints};
String[] prefKeys = {"quizOnePointsYeah", "quizTwoPointsYeah", "quizThreePointsYeah",
"quizFourPointsYeah", "quizFivePointsYeah", "quizSixPointsYeah", "quizSevenPointsYeah",
"quizEightPointsYeah", "quizNinePointsYeah", "quizTenPointsYeah"};
SharedPreferences shareInt = getApplicationContext().getSharedPreferences(QUIZ, MODE_PRIVATE);
for (int i = 0; i < 10; i++)
{
if (shareInt.getInt(prefKeys[i], 0) > 0)
{
points[i] = shareInt.getInt(prefKeys[i], 0);
}//end if
else
{
points[i] = 0;
}//end else
}//end for loop
//Sum up points from each quiz, and put sum in totalPoints variable
totalPoints = numberQuizOnePoints + numberQuizTwoPoints + numberQuizThreePoints
+ numberQuizFourPoints + numberQuizFivePoints + numberQuizSixPoints
+ numberQuizSevenPoints + numberQuizEightPoints + numberQuizNinePoints
+ numberQuizTenPoints;
quizOnePointsText.setText(String.format("%d", numberQuizOnePoints));
quizTwoPointsText.setText(String.format("%d", numberQuizTwoPoints));
quizThreePointsText.setText(String.format("%d", numberQuizThreePoints));
quizFourPointsText.setText(String.format("%d", numberQuizFourPoints));
quizFivePointsText.setText(String.format("%d", numberQuizFivePoints));
quizSixPointsText.setText(String.format("%d", numberQuizSixPoints));
quizSevenPointsText.setText(String.format("%d", numberQuizSevenPoints));
quizEightPointsText.setText(String.format("%d", numberQuizEightPoints));
quizNinePointsText.setText(String.format("%d", numberQuizNinePoints));
quizTenPointsText.setText(String.format("%d", numberQuizTenPoints));
actualPointsText.setText(String.format("%d", totalPoints));
}//end void arrayMethod
}//end class MainMenu
我不確定自己是否知道自己想要什么,但是根據我的理解,您應該在已知會被調用的狀態(例如onPause();
加載和保存數據onPause();
。 您還應該看看Android Activity生命周期
這是我應用中的一些相關片段
@Override
protected void onPause(){
super.onPause();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState); // the UI component values are saved here.
outState.putStringArrayList("mList", new ArrayList<>(fretList));
Toast.makeText(this, "Activity state saved", Toast.LENGTH_SHORT).show();
}
@Override
protected void onRestoreInstanceState(Bundle inState) {
super.onRestoreInstanceState(inState); // the UI component values are saved here.
// if there is a non empty bundle, then pull arraylist from it, clear old list
// just in case, and add all bundle items to it.
if (inState != null) {
ArrayList bundledList = inState.getStringArrayList("mList");
if (bundledList != null && bundledList.size() > 0) {
fretList.clear();
fretList.addAll(bundledList);
}
}
mAdapter.notifyDataSetChanged();
Toast.makeText(this, "Activity state restored", Toast.LENGTH_SHORT).show();
}
編輯:我現在正在尋找您的其他鏈接問題,以更好地理解您的問題。 您可以使用Log.d()
做一些簡單的檢查,以找出丟失包/數據的位置
if (getFromQuizOne != null)
{
Log.d("Test", "not null");
} else {
Log.d("Test", "null");
}
如果您想顯式保存某些內容而無需等待,可以將其移至自己的方法並在需要時調用它(這是使用共享首選項而不是捆綁軟件):
public void save(Context context, String text) {
SharedPreferences settings;
SharedPreferences.Editor editor;
settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
editor = settings.edit();
editor.putString("someString", text);
editor.commit();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.