Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ember.js/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Android应用程序的屏幕/活动之间移动时保存UI状态_Android_Savestate_Multiscreen - Fatal编程技术网

在Android应用程序的屏幕/活动之间移动时保存UI状态

在Android应用程序的屏幕/活动之间移动时保存UI状态,android,savestate,multiscreen,Android,Savestate,Multiscreen,我正在做一个多屏幕测验应用程序。对于每个问题,我都有一个单独的活动/屏幕。在每个屏幕的底部都有下一个/上一个“按钮”,可导航到下一个/上一个屏幕。请参见带有问题的屏幕的UI示例: 不过我有个问题。假设用户选择问题2的答案,然后单击“上一步”,选择问题1中的答案并单击“下一步” 我想保存问题2的UI状态,这样,如果用户通过单击“上一步”或“下一步”返回某个问题,所选答案将保持不变。 我设法做到的一件事是,当用户单击“previous”时,UI将保持不变,我在清单文件中使用了以下代码: andro

我正在做一个多屏幕测验应用程序。对于每个问题,我都有一个单独的活动/屏幕。在每个屏幕的底部都有下一个/上一个“按钮”,可导航到下一个/上一个屏幕。请参见带有问题的屏幕的UI示例:

不过我有个问题。假设用户选择问题2的答案,然后单击“上一步”,选择问题1中的答案并单击“下一步”

我想保存问题2的UI状态,这样,如果用户通过单击“上一步”或“下一步”返回某个问题,所选答案将保持不变。

我设法做到的一件事是,当用户单击“previous”时,UI将保持不变,我在清单文件中使用了以下代码:

android:launchMode="singleTask"
但是,当用户通过“下一步”来回答问题时,我无法将其保存。以下是我的问题2活动代码:

package com.example.justynagolawska.quizappiteration2;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

public class Question2Activity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_question2);

        ActionBar actionbar = getSupportActionBar();

        // Applies the custom action bar style
        getSupportActionBar().setDisplayOptions(actionbar.DISPLAY_SHOW_CUSTOM);
        getSupportActionBar().setCustomView(R.layout.action_bar);

        // Changes the action bar title
        TextView title = (TextView) getSupportActionBar().getCustomView().findViewById(R.id.action_bar_title);
        title.setText(R.string.q2_name);

        //Getting the intent with score for question 1
        Intent question2Intent = getIntent();
        final int resultQ1 = question2Intent.getIntExtra("q1result", 0);

        // Find the View that shows the next TextView
        TextView nextQuestion = (TextView) findViewById(R.id.next);

        // Set a click listener on that View
        nextQuestion.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when next View is clicked on.
            @Override
            public void onClick(View view) {
                //Getting the answer to question 2 checkbox 1
                CheckBox checkBox1Q2 = (CheckBox) findViewById(R.id.checkbox1Q2);
                boolean isCheckBox1Q2 = checkBox1Q2.isChecked();

                //Getting the answer to question 2 checkbox 2
                CheckBox checkBox2Q2 = (CheckBox) findViewById(R.id.checkbox2Q2);
                boolean isCheckBox2Q2 = checkBox2Q2.isChecked();

                //Getting the answer to question 2 checkbox 3
                CheckBox checkBox3Q2 = (CheckBox) findViewById(R.id.checkbox3Q2);
                boolean isCheckBox3Q2 = checkBox3Q2.isChecked();

                //Calculate Question 2 score
                int resultQ2 = calculateResultQ2(isCheckBox1Q2, isCheckBox2Q2, isCheckBox3Q2);

                Intent question3Intent = new Intent(Question2Activity.this, Question3Activity.class);
                question3Intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

                question3Intent.putExtra ("q1result", resultQ1);
                question3Intent.putExtra ("q2result", resultQ2);
                startActivity(question3Intent);
            }
        });

        // Find the View that shows the next TextView
        TextView previousQuestion = (TextView) findViewById(R.id.previous);

        // Set a click listener on that View
        previousQuestion.setOnClickListener(new View.OnClickListener() {
            // The code in this method will be executed when next View is clicked on.
            @Override
            public void onClick(View view) {
                Intent question1Intent = new Intent(Question2Activity.this, Question1Activity.class);
                startActivity(question1Intent);
            }
        });
    }

    /**
     * Check which checkbox was selected in the question 2
     *
     * @param checkBox1 is whether or not the user checked the checkbox1
     * @param checkBox2 is whether or not the user checked the checkbox2
     * @param checkBox3 is whether or not the user checked the checkbox3
     * @return the score the user got for question 2
     */
    private int calculateResultQ2(boolean checkBox1, boolean checkBox2, boolean checkBox3) {
        int result = 0;
        if (checkBox1 && checkBox2 && checkBox3) {
            result = 1;
        }
        return result;
    }
如果有人能帮助我,我将不胜感激。谢谢大家!

编辑:下面是我使用SharedReferences的工作代码,@tahsinRupam提出的解决方案

package com.example.justynagolawska.quizappiteration2;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

public class Question2Activity extends AppCompatActivity {

SharedPreferences mypref;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_question2);

    final CheckBox checkBox1Q2 = (CheckBox) findViewById(R.id.checkbox1Q2);
    final CheckBox checkBox2Q2 = (CheckBox) findViewById(R.id.checkbox2Q2);
    final CheckBox checkBox3Q2 = (CheckBox) findViewById(R.id.checkbox3Q2);

    mypref = PreferenceManager.getDefaultSharedPreferences(this);

    ActionBar actionbar = getSupportActionBar();

    // Applies the custom action bar style
    getSupportActionBar().setDisplayOptions(actionbar.DISPLAY_SHOW_CUSTOM);
    getSupportActionBar().setCustomView(R.layout.action_bar);

    // Changes the action bar title
    TextView title = (TextView) getSupportActionBar().getCustomView().findViewById(R.id.action_bar_title);
    title.setText(R.string.q2_name);

    //Getting the intent with score for question 1
    Intent question2Intent = getIntent();


    final int resultQ1 = question2Intent.getIntExtra("q1result", 0);

    // Find the View that shows the next TextView
    TextView nextQuestion = (TextView) findViewById(R.id.next);

    // Set a click listener on that View
    nextQuestion.setOnClickListener(new View.OnClickListener() {
        // The code in this method will be executed when next View is clicked on.
        @Override
        public void onClick(View view) {


            //Getting the answer to question 2 checkbox 1
            boolean isCheckBox1Q2 = checkBox1Q2.isChecked();

            //Getting the answer to question 2 checkbox 2
            boolean isCheckBox2Q2 = checkBox2Q2.isChecked();

            //Getting the answer to question 2 checkbox 3
            boolean isCheckBox3Q2 = checkBox3Q2.isChecked();

            //Calculate Question 2 score
            int resultQ2 = calculateResultQ2(isCheckBox1Q2, isCheckBox2Q2, isCheckBox3Q2);

            Intent question3Intent = new Intent(Question2Activity.this, Question3Activity.class);
            question3Intent.putExtra ("q1result", resultQ1);
            question3Intent.putExtra ("q2result", resultQ2);
            startActivity(question3Intent);
        }
    });

    // Find the View that shows the next TextView
    TextView previousQuestion = (TextView) findViewById(R.id.previous);

    // Set a click listener on that View
    previousQuestion.setOnClickListener(new View.OnClickListener() {
        // The code in this method will be executed when next View is clicked on.
        @Override
        public void onClick(View view) {
            Intent question1Intent = new Intent(Question2Activity.this, Question1Activity.class);
            startActivity(question1Intent);
        }
    });
}

/**
 * Check which checkbox was selected in the question 2
 *
 * @param checkBox1 is whether or not the user checked the checkbox1
 * @param checkBox2 is whether or not the user checked the checkbox2
 * @param checkBox3 is whether or not the user checked the checkbox3
 * @return the score the user got for question 2
 */
private int calculateResultQ2(boolean checkBox1, boolean checkBox2, boolean checkBox3) {
    int result = 0;
    if (checkBox1 && checkBox2 && checkBox3) {
        result = 1;
    }
    return result;
}

@Override
protected void onPause() {
    super.onPause();

    //Getting the answer to question 2 checkbox 1
    CheckBox checkBox1Q2 = (CheckBox) findViewById(R.id.checkbox1Q2);
    boolean isCheckBox1Q2 = checkBox1Q2.isChecked();

    //Getting the answer to question 2 checkbox 2
    CheckBox checkBox2Q2 = (CheckBox) findViewById(R.id.checkbox2Q2);
    boolean isCheckBox2Q2 = checkBox2Q2.isChecked();

    //Getting the answer to question 2 checkbox 3
    CheckBox checkBox3Q2 = (CheckBox) findViewById(R.id.checkbox3Q2);
    boolean isCheckBox3Q2 = checkBox3Q2.isChecked();

    if(isCheckBox1Q2 == true){
        mypref.edit().putBoolean("Iscb1Checked", true).apply();
    }
    else if(isCheckBox1Q2 == false){
        mypref.edit().putBoolean("Iscb1Checked", false).apply();
    }

    if(isCheckBox2Q2 == true){
        mypref.edit().putBoolean("Iscb2Checked", true).apply();
    }
    else if(isCheckBox2Q2 == false){
        mypref.edit().putBoolean("Iscb2Checked", false).apply();
    }

    if(isCheckBox3Q2 == true){
        mypref.edit().putBoolean("Iscb3Checked", true).apply();
    }
    else if(isCheckBox3Q2 == false){
        mypref.edit().putBoolean("Iscb3Checked", false).apply();
    }

}

@Override
protected void onResume() {
    super.onResume();

    //Getting the answer to question 2 checkbox 1
    CheckBox checkBox1Q2 = (CheckBox) findViewById(R.id.checkbox1Q2);

    //Getting the answer to question 2 checkbox 2
    CheckBox checkBox2Q2 = (CheckBox) findViewById(R.id.checkbox2Q2);

    //Getting the answer to question 2 checkbox 3
    CheckBox checkBox3Q2 = (CheckBox) findViewById(R.id.checkbox3Q2);

    if(mypref.contains("Iscb1Checked")){
        if(mypref.getBoolean("Iscb1Checked",false)){
            checkBox1Q2.setChecked(true);
        }
    }

    if(mypref.contains("Iscb2Checked")){
        if(mypref.getBoolean("Iscb2Checked",false)){
            checkBox2Q2.setChecked(true);
        }
    }

    if(mypref.contains("Iscb3Checked")){
        if(mypref.getBoolean("Iscb3Checked",false)){
            checkBox3Q2.setChecked(true);
        }
    }
}

}
请注意,我在onPause()中替换了以下内容;方法,因为我正在获取null异常:

checkBox1Q2.isChecked()
!checkBox1Q2.isChecked()


您可以使用SharedReference来存储您的值

1) 公开声明复选框和SharedReference:

 CheckBox checkBox1Q2;
 CheckBox checkBox2Q2;
 CheckBox checkBox3Q2;
 SharedPreferences mypref;
2) 在onCreate()中,初始化SharedReference:

 mypref = PreferenceManager.getDefaultSharedPreferences(this);
3) 保存onPause()中的复选框状态(按下后退按钮时会调用cause onPause()。您也可以在onStop()或onDestroy()中声明

4) 获取onResume()方法中的复选框状态:


现在,您将在onClick中获得复选框视图。尽量避免这样。始终在onCreate的初始部分(在setContentView之后)获取视图(findViewById)。

为什么使用
android:launchMode=“singleTask”
?在用户单击“上一步”时保存UI状态。它完成了我需要完成的工作的一半,但当然不是我想要实现的。如果我找到上述问题的有效解决方案,我根本不需要使用它。我认为如果使用viewpager,这将解决您的问题,它将保存用户界面更改,直到销毁。您可以使用saveInstanceState或SharedReference来满足您的需求。Hi@tahsinRupam。谢谢你的建议。您能告诉我如何应用saveInstanceState的更多详细信息吗?我试着这样做,但我想这只是在我旋转屏幕时保存UI状态?嗨@tahsinRupam,不知为什么,它对我不起作用。我在onCreate()之前应用了完全相同的代码;方法。你能告诉我有什么问题吗?我也看到了这篇文章:声称savedInstanceState仅适用于您在同一活动中的情况,例如屏幕旋转?也许分享经验会是一个更好的主意,你认为呢?是的,你是绝对正确的!我读了各种各样的意见后感到困惑。当系统出于资源目的或屏幕旋转而终止活动时,捆绑数据非常有用。但是,当某个活动由于应用程序行为(如按下后退按钮)而被终止时,该活动实例将永久消失。所以saveInstanceState在这里不起作用。在这件事上使用SharedReference是明智的。如果你愿意,我会编辑我的答案,用SharedReference解决这个问题。那太好了!非常感谢。Stackoverflow会因为这个广泛的评论块而杀了我;)但我想感谢你为这个问题付出的努力和时间!
 mypref = PreferenceManager.getDefaultSharedPreferences(this);
  @Override
protected void onPause() {
    super.onPause();
    if(checkBox1Q2.isChecked()){
        mypref.edit().putBoolean("Iscb1Checked", true).apply();
    }
    else if(!checkBox1Q2.isChecked()){
        mypref.edit().putBoolean("Iscb1Checked", false).apply();
    }

    if(checkBox2Q2.isChecked()){
        mypref.edit().putBoolean("Iscb2Checked", true).apply();
    }
    else if(!checkBox2Q2.isChecked()){
        mypref.edit().putBoolean("Iscb2Checked", false).apply();
    }

    if(checkBox3Q2.isChecked()){
        mypref.edit().putBoolean("Iscb3Checked", true).apply();
    }
    else if(!checkBox3Q2.isChecked()){
        mypref.edit().putBoolean("Iscb3Checked", false).apply();
    }


}
@Override
protected void onResume() {
    super.onResume();
        if(mypref.contains("Iscb1Checked")){
            if(mypref.getBoolean("Iscb1Checked",false)){
                checkBox1Q2.setChecked(true);
            }
        }

        if(mypref.contains("Iscb2Checked")){
            if(mypref.getBoolean("Iscb2Checked",false)){
                checkBox2Q2.setChecked(true);
            }
        }

        if(mypref.contains("Iscb3Checked")){
             if(mypref.getBoolean("Iscb3Checked",false)){
                checkBox3Q2.setChecked(true);
             }
         }
}