简体   繁体   English

为什么按下“后退”按钮后未调用onSaveInstanceState()?

[英]Why is onSaveInstanceState() not called after pressing “back” button?

I'm creating a bowling score application which will include a bingo game. 我正在创建一个保龄球得分应用程序,其中将包括一个宾果游戏。 To simplify, at the Score class, there is a button which will start a new activity (Bingo class). 为简化起见,在Score类中,有一个按钮将启动一个新活动(Bingo类)。 While in the game, for sure it is needed to go back and forth from Score to Bingo. 在游戏中,请确保需要从Score到Bingo来回移动。 The problem that I'm facing right now is the onSaveInstanceState is not called when I tried to "back" from Bingo to Score. 我现在正面临的问题是,当我尝试从Bingo“返回”到Score时,没有调用onSaveInstanceState。 Hence, the bingo numbers that should be fixed is changing every time I click button of "Game" which start the Bingo. 因此,每次我单击启动“宾果游戏”的“游戏”按钮时,应该固定的宾果游戏号码都会改变。

I had been searching and trying all of the related answers (including https://developer.android.com/guide/components/activities/activity-lifecycle ), but nothing works for me. 我一直在搜索并尝试所有相关的答案(包括https://developer.android.com/guide/components/activities/activity-lifecycle ),但对我来说没有任何用处。 I also tried to temporarily remove the screenOrientation on any activity as I read that screen rotation will create a new instance. 我还尝试在任何活动上临时删除screenOrientation,因为我看到屏幕旋转将创建一个新实例。 Here is my code that related to the problem. 这是与该问题有关的代码。

Score.java (button to move the Bingo) Score.java(移动宾果游戏的按钮)

 @Override
    public void onClick(View view) {

        String button_text;
        button_text = ((Button) view).getText().toString();

        if (button_text.equals("GAME")) {
            Intent gamePage = new Intent(this, Game.class);
            startActivity(gamePage);
        }

Game.java (bingo1 and bingo2 is the array of integer) Game.java(bingo1和bingo2是整数数组)

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

        ...

        if (savedInstanceState != null){
            bingo1 = savedInstanceState.getIntArray("bingo1");
            bingo2 = savedInstanceState.getIntArray("bingo2");
        }else {

            ...


     }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {

        savedInstanceState.putIntArray("bingo1", bingo1);
        savedInstanceState.putIntArray("bingo2", bingo2);

        super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {

        super.onRestoreInstanceState(savedInstanceState);


        bingo1 = savedInstanceState.getIntArray("array1");
        bingo2 = savedInstanceState.getIntArray("array2");
    }

Correct me if by calling in onCreate() and onRestoreInstanceState at the same time is wrong. 如果同时调用onCreate()和onRestoreInstanceState是错误的,请更正我。

As stated earlier, I tried to have the bingo number fix throughout the back and forth movement from Score to Game/Bingo, which I failed to achieve by using these code. 如前所述,我试图在从得分到游戏/宾果游戏的来回移动中修复宾果游戏号码,但我无法使用这些代码来实现。 I don't know if there are more code which related to the problem as I'm still new, but it will be really helpful if you guys can help. 我还不熟悉,所以我不知道是否有更多与此问题相关的代码,但是如果你们可以提供帮助,它将非常有帮助。 Thank you! 谢谢!

I am assuming that Bingo and Score are both activities. 我假设Bingo和Score都是活动。

The problem that I'm facing right now is the onSaveInstanceState is not called when I tried to "back" from Bingo to Score 我现在正面临的问题是,当我尝试从Bingo“返回”到Score时,未调用onSaveInstanceState

It is not supposed to be called then. 不应调用它。 Your Bingo activity instance is being permanently destroyed. 您的Bingo活动实例将被永久销毁。 It has no more instance state to save. 它没有更多实例状态可保存。

If Bingo and Score are supposed to share common state, then perhaps they should not be separate activities. 如果Bingo和Score应该具有共同的状态,那么也许它们不应该是分开的活动。 Instead, use one activity, two fragments (Bingo and Score), with a shared ViewModel between them. 而是使用一个活动,两个片段(Bingo和Score),并在它们之间共享一个ViewModel

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM