活动呼叫本身 - :按下后退按钮时清除共享偏好设置

时间:2015-12-12 12:15:23

标签: android sharedpreferences

我正在使用以下不断调用自身的活动。当我向前进时,情况正常,但是当我点击后退按钮时,共享的偏好就会被清除。

   SharedPreferences userLocalDatabase;
 protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_paper_dashboard);

            userLocalDatabase = PreferenceManager.getDefaultSharedPreferences(this);
            name = userLocalDatabase.getString("name", "");
            subject = userLocalDatabase.getString("subject", "");
            timeLeft = userLocalDatabase.getInt("timeLeft", -1);
            quesNo = userLocalDatabase.getInt("quesNo", -1);
            no_of_ques = userLocalDatabase.getInt("no_of_ques", -1);
            score = userLocalDatabase.getInt("score", -1);
            currentLine = quesNo+1;

            Log.i("TAG2", "Krishna the score returned from Shared Preferences is -: " +score);

            myQuesTitle = (TextView)findViewById(R.id.myQuesTitle);
            myTimer = (TextView)findViewById(R.id.myTimer);
            myQuestion = (TextView)findViewById(R.id.myQuestion);

            myFinish = (ImageButton)findViewById(R.id.myFinish);
            myPrevious = (ImageButton)findViewById(R.id.myPrevious);
            myAnswer = (ImageButton)findViewById(R.id.myAnswer);
            myHint = (ImageButton)findViewById(R.id.myHint);
            myNext = (ImageButton)findViewById(R.id.myNext);

            myAnswer1 = (CheckBox)findViewById(R.id.myAnswer1);
            myAnswer2 = (CheckBox)findViewById(R.id.myAnswer2);
            myAnswer3 = (CheckBox)findViewById(R.id.myAnswer3);
            myAnswer4 = (CheckBox)findViewById(R.id.myAnswer4);

            String line = setLayout(name, timeLeft, quesNo, no_of_ques);
            Log.i("TAG2", "Krishna the line returned is -: " + line);
        }

这是调用的方法 - :

   public void clickPrevious(View v){

   calculateScore();
    SharedPreferences.Editor spEditor = userLocalDatabase.edit();
    spEditor.putString("name", name);
    spEditor.putString("subject", subject);
    spEditor.putInt("timeLeft", timeLeft);
    spEditor.putInt("no_of_ques", no_of_ques);
    spEditor.putInt("quesNo", 0);
    spEditor.putInt("score", 0 );
    spEditor.commit();
    Log.i("TAG2", "Baladeva score is -: " + score);
    finish();
    startActivity(new Intent(paper_dashboard.this, paper_dashboard.class));
}

发布Next方法 - :

    public void clickNext(View v){

            calculateScore();
            SharedPreferences.Editor spEditor = userLocalDatabase.edit();
            spEditor.putString("name", name);
            spEditor.putString("subject", subject);
            spEditor.putInt("timeLeft", timeLeft);
            spEditor.putInt("no_of_ques", no_of_ques);
            spEditor.putInt("quesNo", quesNo+1);
            spEditor.putInt("score", score);
            spEditor.commit();
            Log.i("TAG2", "Baladeva score is -: " + score);
            Intent intent = new Intent(new Intent(this, paper_dashboard.class));
            //intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }

0 个答案:

没有答案