Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

c# - 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). 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"); 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.

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);          
        }
    }
question from:https://stackoverflow.com/questions/65948105/itineration-problem-with-function-after-resume-app

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...