简体   繁体   English

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

[英]itineration problem with function after resume app

this probably is a noob question, but I can't figure out how resolve it.这可能是一个菜鸟问题,但我不知道如何解决它。 Im opening/resuming my android app from a notification (intent data).我从通知(意图数据)打开/恢复我的 android 应用程序。 the app is opening fine from the intent.该应用程序可以正常打开。 the problem is if send the app to the background again after opening from the intent and later I resume again, the coroutine executes again every time I resume, because keeps getting the data from the "old" intent.问题是如果从意图打开后再次将应用程序发送到后台,然后我再次恢复,则每次恢复时协程都会再次执行,因为不断从“旧”意图获取数据。 *is there some way of clear the intent data? *有什么方法可以清除意图数据吗? (without close the app) im trying something like: AndroidJavaObject saveIntent = curActivity.Call<AndroidJavaObject>("setData"); (不关闭应用程序)我正在尝试类似: AndroidJavaObject saveIntent = curActivity.Call<AndroidJavaObject>("setData"); to replace the intent data so the next itineration dont get te correct values替换意图数据,以便下一次迭代不会得到正确的值

*Im thinking to limit to 1, the times what the coroutine can be executed, but not is a "clean"solution. *我想限制为 1,协程可以执行的次数,但不是“干净”的解决方案。

can someone give me some guidance?.有人可以给我一些指导吗? this is my code so far:到目前为止,这是我的代码:

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);          
        }
    }

this is working for me now:这现在对我有用:

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