繁体   English   中英

恢复应用程序后 function 的迭代问题

[英]itineration problem with function after resume app

这可能是一个菜鸟问题,但我不知道如何解决它。 我从通知(意图数据)打开/恢复我的 android 应用程序。 该应用程序可以正常打开。 问题是如果从意图打开后再次将应用程序发送到后台,然后我再次恢复,则每次恢复时协程都会再次执行,因为不断从“旧”意图获取数据。 *有什么方法可以清除意图数据吗? (不关闭应用程序)我正在尝试类似: AndroidJavaObject saveIntent = curActivity.Call<AndroidJavaObject>("setData"); 替换意图数据,以便下一次迭代不会得到正确的值

*我想限制为 1,协程可以执行的次数,但不是“干净”的解决方案。

有人可以给我一些指导吗? 到目前为止,这是我的代码:

void OnApplicationPause(bool appPaused)
    {
        if (!isOnAndroid || Application.isEditor) { return; }

        if (!appPaused)
        {
            //Returning to Application
            Debug.Log("Application Resumed");
            StartCoroutine(LoadSceneFromFCM());
        }
        else
        {
            //Leaving Application
            Debug.Log("Application Paused");
        }
    }

    IEnumerator LoadSceneFromFCM()
    {
        AndroidJavaClass UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject curActivity = UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
        AndroidJavaObject curIntent = curActivity.Call<AndroidJavaObject>("getIntent");

        string sceneToLoad = curIntent.Call<string>("getStringExtra", "sceneToOpen");
        //string extraInfo = curIntent.Call<string>("getStringExtra", "extraInfo"); use this for do some extra stuff

        Scene curScene = SceneManager.GetActiveScene();

        if (!string.IsNullOrEmpty(sceneToLoad) && sceneToLoad != curScene.name)
        {
            // If the current scene is different than the intended scene to load,
            // load the intended scene. This is to avoid reloading an already acive
            // scene.
            Debug.Log("Loading Scene: " + sceneToLoad);
            Handheld.SetActivityIndicatorStyle(AndroidActivityIndicatorStyle.Large);
            Handheld.StartActivityIndicator();
            yield return new WaitForSeconds(0f);
            SceneManager.LoadScene(sceneToLoad);          
        }
    }

这现在对我有用:

void Awake()
{
    DontDestroyOnLoad(gameObject);
}

void OnApplicationPause(bool appPaused)
{
    AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
    AndroidJavaObject currentActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");
    AndroidJavaObject intent = currentActivity.Call<AndroidJavaObject>("getIntent");

    Scene curScene = SceneManager.GetActiveScene();

    string sceneToLoad = intent.Call<string>("getStringExtra", "sceneToOpen");
    if (sceneToLoad != null && sceneToLoad.Trim().Length > 0 && sceneToLoad != curScene.name)
    {
        Debug.Log("Load the Video Scene");
        SceneManager.LoadScene(sceneToLoad);
        //redirectToAppropriateScreen(intent, onClickScreen);
    }
    intent.Call("removeExtra", "sceneToOpen"); //this remove the data from the intent, so the function cant be completed after resume again.
    Debug.Log("Extra data removed");
}

暂无
暂无

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

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