简体   繁体   English

如何从特定片段的活动中禁用 onDestroy 方法?

[英]How to disable an onDestroy method from activity in a specific fragment?

I have an activity with multiple fragments.我有一个包含多个片段的活动。 When the user reaches the last fragment and clicks on a button, he gets to the MainActivity via intent.当用户到达最后一个片段并单击按钮时,他会通过意图进入 MainActivity。 This is a multiplayer quiz app and I need to update a "turn" field in the database which decides wether player 1 or 2 has to play.这是一个多人问答应用程序,我需要更新数据库中的“回合”字段,该字段决定玩家 1 或 2 是否必须玩。 This field currently gets updated when...此字段当前在以下时间更新...

... the player finishes his quiz round normally (by clicking the next button on the last fragment) ...玩家正常完成他的测验回合(通过单击最后一个片段上的下一个按钮)

... the player clicks the back button of the phone during the game (it opens an AlertDialog so when the player chooses to go back to the MainActivity this triggers an "update turn field method") ...玩家在游戏过程中点击手机的后退按钮(它会打开一个AlertDialog ,因此当玩家选择返回 MainActivity 时会触发“更新转场方法”)

There has to be a third option where the turn field gets updated and this is the scenario: The player starts his turn but closes the app during his turn.必须有第三个选项来更新转场,这是场景:玩家开始他的回合,但在他的回合期间关闭应用程序。 This means currently that the "turn" field in my database does not get updated which means in my code that neither of the players can resume the game.这意味着目前我的数据库中的“转弯”字段没有得到更新,这意味着在我的代码中两个玩家都不能继续游戏。 I want to avoid this by updating the turn field when the user decides to close the app.我想通过在用户决定关闭应用程序时更新转向字段来避免这种情况。

In the fragments there is a ProgressBar which is still running when the user accidentally closes the app, I did this in the onStop method of the fragment.在片段中有一个ProgressBar ,当用户意外关闭应用程序时它仍在运行,我在片段的onStop方法中这样做了。 Because of this I cannot call the onStop method in the activity to update the fields in the database, so I am wondering if there is a way to achieve my desired result without calling the onStop method.因此我无法在活动中调用onStop方法来更新数据库中的字段,所以我想知道是否有一种方法可以在不调用 onStop 方法的情况下实现我想要的结果。

Here is my activity where I have currently the method that updates some fields in Firestore inside the onDestroy method:这是我的活动,我目前有在 onDestroy 方法内更新 Firestore 中某些字段的方法:

...
// When the user clicks the phones back button during the game an alert window opens to aks if he is sure to exit the game
    public void onBackPressed() {
        AlertDialog();
    }

public void AlertDialog() {

        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeGame();
                Intent i = new Intent(GameActivity.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        });
    }

 @Override
    protected void onDestroy() {
        super.onDestroy();
        updateGameStatus();
    }

    protected void updateGameStatus() {

collectionRef.document(gameId).collection("Round").document("Round").update("round", roundValue + 1)
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                }
                            });

        }
...

Is there a good way to achieve my desired result?有什么好的方法可以达到我想要的结果吗?

Please let me know if you need additional informations.如果您需要其他信息,请告诉我。

I very much appreciate any tips!我非常感谢任何提示!

Since there is no way to disable the onDestroy on a specific Fragment, I arranged my code logic around the onDestroy method of the Activity that holds the Fragments.由于无法禁用特定 Fragment 上的 onDestroy,因此我围绕保存 Fragment 的 Activity 的onDestroy方法安排了我的代码逻辑。

I have an activity with multiple fragments.我有一个包含多个片段的活动。 When the user reaches the last fragment and clicks on a button, he gets to the MainActivity via intent.当用户到达最后一个片段并点击一个按钮时,他通过意图进入 MainActivity。 This is a multiplayer quiz app and I need to update a "turn" field in the database which decides wether player 1 or 2 has to play.这是一个多人测验应用程序,我需要更新数据库中的“回合”字段,该字段决定玩家 1 还是 2 必须玩。 This field currently gets updated when...此字段当前会在...

... the player finishes his quiz round normally (by clicking the next button on the last fragment) ...玩家正常完成他的测验回合(通过单击最后一个片段上的下一个按钮)

... the player clicks the back button of the phone during the game (it opens an AlertDialog so when the player chooses to go back to the MainActivity this triggers an "update turn field method") ...玩家在游戏过程中点击手机的后退按钮(它会打开一个AlertDialog所以当玩家选择返回 MainActivity 时,这会触发“更新回合方法”)

There has to be a third option where the turn field gets updated and this is the scenario: The player starts his turn but closes the app during his turn.必须有第三个选项来更新回合字段,这就是场景:玩家开始他的回合,但在他的回合中关闭应用程序。 This means currently that the "turn" field in my database does not get updated which means in my code that neither of the players can resume the game.这意味着目前我的数据库中的“turn”字段没有更新,这意味着在我的代码中,两个玩家都不能恢复游戏。 I want to avoid this by updating the turn field when the user decides to close the app.我想通过在用户决定关闭应用程序时更新转弯字段来避免这种情况。

In the fragments there is a ProgressBar which is still running when the user accidentally closes the app, I did this in the onStop method of the fragment.在片段中有一个ProgressBar当用户不小心关闭应用程序时它仍在运行,我在片段的onStop方法中做到了这一点。 Because of this I cannot call the onStop method in the activity to update the fields in the database, so I am wondering if there is a way to achieve my desired result without calling the onStop method.因此我无法在活动中调用onStop方法来更新数据库中的字段,所以我想知道是否有一种方法可以在不调用 onStop 方法的情况下实现我想要的结果。

Here is my activity where I have currently the method that updates some fields in Firestore inside the onDestroy method:这是我的活动,我目前拥有在 onDestroy 方法中更新 Firestore 中某些字段的方法:

...
// When the user clicks the phones back button during the game an alert window opens to aks if he is sure to exit the game
    public void onBackPressed() {
        AlertDialog();
    }

public void AlertDialog() {

        yes.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                closeGame();
                Intent i = new Intent(GameActivity.this, MainActivity.class);
                startActivity(i);
                finish();
            }
        });
    }

 @Override
    protected void onDestroy() {
        super.onDestroy();
        updateGameStatus();
    }

    protected void updateGameStatus() {

collectionRef.document(gameId).collection("Round").document("Round").update("round", roundValue + 1)
                            .addOnCompleteListener(new OnCompleteListener<Void>() {
                                @Override
                                public void onComplete(@NonNull Task<Void> task) {
                                }
                            });

        }
...

Is there a good way to achieve my desired result?有什么好的方法可以达到我想要的结果吗?

Please let me know if you need additional informations.如果您需要其他信息,请告诉我。

I very much appreciate any tips!我非常感谢任何提示!

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

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