在Android应用程序中保存屏幕旋转变量

时间:2017-03-06 13:58:18

标签: java android

当我在我的Android应用程序中旋转屏幕时,变量(犯规,目标等)会被重置。我希望他们不要在屏幕旋转时重置。我能怎么做? 我知道问题与活动生命周期有关,但是我无法解决问题,因为我从非常短的时间(2个月)开始。 这是var applyBoxes2 = from box in driver.FindElements(By.CssSelector("div.col-apply.col-md-1")) let clickableBox = box.FindElement(By.TagName("input")) where box.Text != "Apply?" && clickableBox?.Displayed = True select clickableBox;

MainActivity.java

感谢您帮助我。

1 个答案:

答案 0 :(得分:0)

我解决了,通过将变量放在方法onSaveInstanceState的bundle savedInstanceState中来保存变量,然后再从方法onResumeInstanceState中的bundle中获取它们。这是java代码:

// Saves variables in Bundle savedInstanceState
    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putInt("GoalsForTeamA", goalsForTeamA);
        savedInstanceState.putInt("GoalsForTeamB", goalsForTeamB);
        savedInstanceState.putInt("ShotsForTeamA", shotsForTeamA);
        savedInstanceState.putInt("ShotsForTeamB", shotsForTeamB);
        savedInstanceState.putInt("CornersForTeamA", cornersForTeamA);
        savedInstanceState.putInt("CornersForTeamB", cornersForTeamB);
        savedInstanceState.putInt("FoulsForTeamA", foulsForTeamA);
        savedInstanceState.putInt("FoulsForTeamB", foulsForTeamB);
        savedInstanceState.putInt("YellowCardsForTeamA", yellowCardsForTeamA);
        savedInstanceState.putInt("YellowCardsForTeamB", yellowCardsForTeamB);
        savedInstanceState.putInt("RedCardsForTeamA", redCardsForTeamA);
        savedInstanceState.putInt("RedCardsForTeamB", redCardsForTeamB);
        savedInstanceState.putString("ScorerNames",scorerNames );
        savedInstanceState.putString("LastScorer", lastScorer );
    }
// Gets variables from Bundle savedInstanceState and displays them again.
    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        goalsForTeamA = savedInstanceState.getInt("GoalsForTeamA");
        goalsForTeamB = savedInstanceState.getInt("GoalsForTeamB");
        shotsForTeamA = savedInstanceState.getInt("ShotsForTeamA");
        shotsForTeamB = savedInstanceState.getInt("ShotsForTeamB");
        foulsForTeamA = savedInstanceState.getInt("FoulsForTeamA");
        foulsForTeamB = savedInstanceState.getInt("FoulsForTeamB");
        cornersForTeamA = savedInstanceState.getInt("CornersForTeamA");
        cornersForTeamB = savedInstanceState.getInt("CornersForTeamB");
        yellowCardsForTeamA = savedInstanceState.getInt("YellowCardsForTeamA");
        yellowCardsForTeamB = savedInstanceState.getInt("YellowCardsForTeamB");
        redCardsForTeamA = savedInstanceState.getInt("RedCardsForTeamA");
        redCardsForTeamB = savedInstanceState.getInt("RedCardsForTeamB");
        scorerNames = savedInstanceState.getString("ScorerNames");
        lastScorer = savedInstanceState.getString("LastScorer");
        displayGoalsForTeamA(goalsForTeamA);
        displayGoalsForTeamB(goalsForTeamB);
        displayShotsForTeamA(shotsForTeamA);
        displayShotsForTeamB(shotsForTeamB);
        displayFoulsForTeamA(foulsForTeamA);
        displayFoulsForTeamB(foulsForTeamB);
        displayCornersForTeamA(cornersForTeamA);
        displayCornersForTeamB(cornersForTeamB);
        displayYellowCardsForTeamA(yellowCardsForTeamA);
        displayYellowCardsForTeamB(yellowCardsForTeamB);
        displayRedCardsForTeamA(redCardsForTeamA);
        displayRedCardsForTeamB(redCardsForTeamB);
        displayScorers(scorerNames);
    }