簡體   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