简体   繁体   English

如何为多个活动恢复屏幕活动?

[英]How do I resume screen activity for more than one activity?

So I have these two screens, Messages screen and Game screen.所以我有这两个屏幕,消息屏幕和游戏屏幕。 What I want to do is when the user switches back from either screen, it resumes from the previous activity.我想要做的是当用户从任一屏幕切换回来时,它会从上一个活动恢复。

This is a bit of code in my Game screen that allows the user to go to the Messages screen:这是我的游戏屏幕中的一些代码,允许用户转到消息屏幕:

public void onMessagesButtonClick() {
    Intent intent = new Intent(this, Messages.class);
    startActivity(intent);
    overridePendingTransition(0, 0);
}

And this is a bit of code that allows the user to go from the Messages screen back to the Game screen.这是一段允许用户从消息屏幕返回到游戏屏幕的代码。

public void onGameButtonClick() {
    Intent intent = new Intent(getApplicationContext(), Game.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);
    finish();
    overridePendingTransition(0, 0);
}

So far I am able to go from the Game screen to the Messages screen, then go from the Messages screen and resume activity in the Game screen.到目前为止,我可以从游戏屏幕转到消息屏幕,然后从消息屏幕转到游戏屏幕中的活动。

The problem I now have is I can't resume activity from the Messages screen, only the Game screen.我现在遇到的问题是我无法从消息屏幕恢复活动,只能从游戏屏幕恢复活动。 If I were to update the onMessagesButtonClick() to:如果我要将onMessagesButtonClick()更新为:

public void onMessagesButtonClick() {
    Intent intent = new Intent(getApplicationContext(), Messages.class);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    startActivity(intent);
    finish();
    overridePendingTransition(0, 0);
}

I will no longer be able to resume activity from the Game screen.我将无法再从游戏屏幕恢复活动。 It will be as if I am entering the screen for the first time, starting the screen fresh.就像我第一次进入屏幕一样,重新启动屏幕。

What can I do to be able to resume previous activity from both screens when going back and forth between screens?在屏幕之间来回切换时,我该怎么做才能从两个屏幕恢复以前的活动?

try this one 试试这个

public void onGameButtonClick() {
    Intent intent = new Intent();
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
    setResult(RESULT_OK, intent);
    finish();
    overridePendingTransition(0, 0);
}

on your game class 在你的游戏课上

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if(resultCode == RESULT_OK) {
             //If you are going to fetch data from former activity
            }
    }
}

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

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