Cache is empty after the Activity is returned












0















I am having this issue on an app that I made. It's a simple game that I loads data from Room database in my onCreate method, saves the loaded data in List<GameData> gameData and then starts the game.



gameDao.getAll()
.subscribe(gameDatas -> {
gameData.addAll(gameDatas);
startGame();
});


inside my startGame, I randomly select a number in the range [0, gameData.size()] and do some game related work with the corresponding gameData.



public void startGame(){
Random random = new Random();
int randomNumber = random.nextInt(gameData.size());

// do something with gameData[ramdomNumber]
}


So, then user keeps playing and then when the user loses I send him to GameOverActivity with startActivityForResult(gameOver,1123)



Inside GameOverActivity user can




  • restart the game

  • watch an ad to continue playing

  • other social related staff


So when the user presses restart button, I just finish the GameOverActivity which will redirect me to onActivityResult of GameActivity. Here I just call startGame method again, and it should supposedly start a new game.



Now the problem is sometimes for certain devices, gameData is getting empty and hence I am getting exception inside startGame when I call random.nextInt(gameData.size())



java.lang.RuntimeException: 

at android.app.ActivityThread.performResumeActivity (ActivityThread.java:3790)

at android.app.ActivityThread.handleResumeActivity (ActivityThread.java:3830)

at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3038)

at android.app.ActivityThread.-wrap11 (Unknown Source)

at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1696)

at android.os.Handler.dispatchMessage (Handler.java:105)

at android.os.Looper.loop (Looper.java:164)

at android.app.ActivityThread.main (ActivityThread.java:6944)

at java.lang.reflect.Method.invoke (Native Method)

at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327)

at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
Caused by: java.lang.RuntimeException:

at android.app.ActivityThread.deliverResults (ActivityThread.java:4491)

at android.app.ActivityThread.performResumeActivity (ActivityThread.java:3762)
Caused by: java.lang.IllegalArgumentException:

at java.util.Random.nextInt (Random.java:388)

at com.company.game.GameActivity.startGame (GameActivity.java:149)

at com.company.game.GameActivity.onActivityResult (GameActivity.java:477)

at android.app.Activity.dispatchActivityResult (Activity.java:7556)

at android.app.ActivityThread.deliverResults (ActivityThread.java:4487)


I am not understating why would gameData is getting cleared, and I am not able to check because I have never encountered while testing and I got the crash report from PlayConsole. Any idea what might be causing this?










share|improve this question

























  • The line gameData.addAll(gameData); makes no sense. Shouldn't it be gameData.addAll(gameDatas); with the "s"?

    – lionscribe
    Nov 25 '18 at 5:18











  • yeah, its a typo

    – musooff
    Nov 25 '18 at 8:24
















0















I am having this issue on an app that I made. It's a simple game that I loads data from Room database in my onCreate method, saves the loaded data in List<GameData> gameData and then starts the game.



gameDao.getAll()
.subscribe(gameDatas -> {
gameData.addAll(gameDatas);
startGame();
});


inside my startGame, I randomly select a number in the range [0, gameData.size()] and do some game related work with the corresponding gameData.



public void startGame(){
Random random = new Random();
int randomNumber = random.nextInt(gameData.size());

// do something with gameData[ramdomNumber]
}


So, then user keeps playing and then when the user loses I send him to GameOverActivity with startActivityForResult(gameOver,1123)



Inside GameOverActivity user can




  • restart the game

  • watch an ad to continue playing

  • other social related staff


So when the user presses restart button, I just finish the GameOverActivity which will redirect me to onActivityResult of GameActivity. Here I just call startGame method again, and it should supposedly start a new game.



Now the problem is sometimes for certain devices, gameData is getting empty and hence I am getting exception inside startGame when I call random.nextInt(gameData.size())



java.lang.RuntimeException: 

at android.app.ActivityThread.performResumeActivity (ActivityThread.java:3790)

at android.app.ActivityThread.handleResumeActivity (ActivityThread.java:3830)

at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3038)

at android.app.ActivityThread.-wrap11 (Unknown Source)

at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1696)

at android.os.Handler.dispatchMessage (Handler.java:105)

at android.os.Looper.loop (Looper.java:164)

at android.app.ActivityThread.main (ActivityThread.java:6944)

at java.lang.reflect.Method.invoke (Native Method)

at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327)

at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
Caused by: java.lang.RuntimeException:

at android.app.ActivityThread.deliverResults (ActivityThread.java:4491)

at android.app.ActivityThread.performResumeActivity (ActivityThread.java:3762)
Caused by: java.lang.IllegalArgumentException:

at java.util.Random.nextInt (Random.java:388)

at com.company.game.GameActivity.startGame (GameActivity.java:149)

at com.company.game.GameActivity.onActivityResult (GameActivity.java:477)

at android.app.Activity.dispatchActivityResult (Activity.java:7556)

at android.app.ActivityThread.deliverResults (ActivityThread.java:4487)


I am not understating why would gameData is getting cleared, and I am not able to check because I have never encountered while testing and I got the crash report from PlayConsole. Any idea what might be causing this?










share|improve this question

























  • The line gameData.addAll(gameData); makes no sense. Shouldn't it be gameData.addAll(gameDatas); with the "s"?

    – lionscribe
    Nov 25 '18 at 5:18











  • yeah, its a typo

    – musooff
    Nov 25 '18 at 8:24














0












0








0








I am having this issue on an app that I made. It's a simple game that I loads data from Room database in my onCreate method, saves the loaded data in List<GameData> gameData and then starts the game.



gameDao.getAll()
.subscribe(gameDatas -> {
gameData.addAll(gameDatas);
startGame();
});


inside my startGame, I randomly select a number in the range [0, gameData.size()] and do some game related work with the corresponding gameData.



public void startGame(){
Random random = new Random();
int randomNumber = random.nextInt(gameData.size());

// do something with gameData[ramdomNumber]
}


So, then user keeps playing and then when the user loses I send him to GameOverActivity with startActivityForResult(gameOver,1123)



Inside GameOverActivity user can




  • restart the game

  • watch an ad to continue playing

  • other social related staff


So when the user presses restart button, I just finish the GameOverActivity which will redirect me to onActivityResult of GameActivity. Here I just call startGame method again, and it should supposedly start a new game.



Now the problem is sometimes for certain devices, gameData is getting empty and hence I am getting exception inside startGame when I call random.nextInt(gameData.size())



java.lang.RuntimeException: 

at android.app.ActivityThread.performResumeActivity (ActivityThread.java:3790)

at android.app.ActivityThread.handleResumeActivity (ActivityThread.java:3830)

at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3038)

at android.app.ActivityThread.-wrap11 (Unknown Source)

at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1696)

at android.os.Handler.dispatchMessage (Handler.java:105)

at android.os.Looper.loop (Looper.java:164)

at android.app.ActivityThread.main (ActivityThread.java:6944)

at java.lang.reflect.Method.invoke (Native Method)

at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327)

at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
Caused by: java.lang.RuntimeException:

at android.app.ActivityThread.deliverResults (ActivityThread.java:4491)

at android.app.ActivityThread.performResumeActivity (ActivityThread.java:3762)
Caused by: java.lang.IllegalArgumentException:

at java.util.Random.nextInt (Random.java:388)

at com.company.game.GameActivity.startGame (GameActivity.java:149)

at com.company.game.GameActivity.onActivityResult (GameActivity.java:477)

at android.app.Activity.dispatchActivityResult (Activity.java:7556)

at android.app.ActivityThread.deliverResults (ActivityThread.java:4487)


I am not understating why would gameData is getting cleared, and I am not able to check because I have never encountered while testing and I got the crash report from PlayConsole. Any idea what might be causing this?










share|improve this question
















I am having this issue on an app that I made. It's a simple game that I loads data from Room database in my onCreate method, saves the loaded data in List<GameData> gameData and then starts the game.



gameDao.getAll()
.subscribe(gameDatas -> {
gameData.addAll(gameDatas);
startGame();
});


inside my startGame, I randomly select a number in the range [0, gameData.size()] and do some game related work with the corresponding gameData.



public void startGame(){
Random random = new Random();
int randomNumber = random.nextInt(gameData.size());

// do something with gameData[ramdomNumber]
}


So, then user keeps playing and then when the user loses I send him to GameOverActivity with startActivityForResult(gameOver,1123)



Inside GameOverActivity user can




  • restart the game

  • watch an ad to continue playing

  • other social related staff


So when the user presses restart button, I just finish the GameOverActivity which will redirect me to onActivityResult of GameActivity. Here I just call startGame method again, and it should supposedly start a new game.



Now the problem is sometimes for certain devices, gameData is getting empty and hence I am getting exception inside startGame when I call random.nextInt(gameData.size())



java.lang.RuntimeException: 

at android.app.ActivityThread.performResumeActivity (ActivityThread.java:3790)

at android.app.ActivityThread.handleResumeActivity (ActivityThread.java:3830)

at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:3038)

at android.app.ActivityThread.-wrap11 (Unknown Source)

at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1696)

at android.os.Handler.dispatchMessage (Handler.java:105)

at android.os.Looper.loop (Looper.java:164)

at android.app.ActivityThread.main (ActivityThread.java:6944)

at java.lang.reflect.Method.invoke (Native Method)

at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:327)

at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1374)
Caused by: java.lang.RuntimeException:

at android.app.ActivityThread.deliverResults (ActivityThread.java:4491)

at android.app.ActivityThread.performResumeActivity (ActivityThread.java:3762)
Caused by: java.lang.IllegalArgumentException:

at java.util.Random.nextInt (Random.java:388)

at com.company.game.GameActivity.startGame (GameActivity.java:149)

at com.company.game.GameActivity.onActivityResult (GameActivity.java:477)

at android.app.Activity.dispatchActivityResult (Activity.java:7556)

at android.app.ActivityThread.deliverResults (ActivityThread.java:4487)


I am not understating why would gameData is getting cleared, and I am not able to check because I have never encountered while testing and I got the crash report from PlayConsole. Any idea what might be causing this?







android caching random crash android-lifecycle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 8:24







musooff

















asked Nov 25 '18 at 3:53









musooffmusooff

496212




496212













  • The line gameData.addAll(gameData); makes no sense. Shouldn't it be gameData.addAll(gameDatas); with the "s"?

    – lionscribe
    Nov 25 '18 at 5:18











  • yeah, its a typo

    – musooff
    Nov 25 '18 at 8:24



















  • The line gameData.addAll(gameData); makes no sense. Shouldn't it be gameData.addAll(gameDatas); with the "s"?

    – lionscribe
    Nov 25 '18 at 5:18











  • yeah, its a typo

    – musooff
    Nov 25 '18 at 8:24

















The line gameData.addAll(gameData); makes no sense. Shouldn't it be gameData.addAll(gameDatas); with the "s"?

– lionscribe
Nov 25 '18 at 5:18





The line gameData.addAll(gameData); makes no sense. Shouldn't it be gameData.addAll(gameDatas); with the "s"?

– lionscribe
Nov 25 '18 at 5:18













yeah, its a typo

– musooff
Nov 25 '18 at 8:24





yeah, its a typo

– musooff
Nov 25 '18 at 8:24












1 Answer
1






active

oldest

votes


















0














I suspect the problem happens when the OS destroys the GamaActivity due to memory needs, and then recreates it for the onActivityResult. This is part of the normal Android Activity Lifecycle. Now when that happens, gameData will be null, and onCreate is called to init. onCreate will call again the Room function to fill gameData, but as Room is asynchronous, it will let onCreate finish, before the gameData is filled. At that point, onActivityResult will be called, and gameData may still empty, as the the Room code hasn't finished, so app crashes.
The correct way to fix this, is to move gameData out of the Activity class, and to the Application class. That is where it belongs, as data should be tied to the Application, not the Activity. You will have to extend the Application class, and in the onCreate of the Application, init the Room data. Just Google "Extending Android Application Class" for specific instructions. This way the data will be safe for the lifetime of the application.






share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464512%2fcache-is-empty-after-the-activity-is-returned%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    I suspect the problem happens when the OS destroys the GamaActivity due to memory needs, and then recreates it for the onActivityResult. This is part of the normal Android Activity Lifecycle. Now when that happens, gameData will be null, and onCreate is called to init. onCreate will call again the Room function to fill gameData, but as Room is asynchronous, it will let onCreate finish, before the gameData is filled. At that point, onActivityResult will be called, and gameData may still empty, as the the Room code hasn't finished, so app crashes.
    The correct way to fix this, is to move gameData out of the Activity class, and to the Application class. That is where it belongs, as data should be tied to the Application, not the Activity. You will have to extend the Application class, and in the onCreate of the Application, init the Room data. Just Google "Extending Android Application Class" for specific instructions. This way the data will be safe for the lifetime of the application.






    share|improve this answer






























      0














      I suspect the problem happens when the OS destroys the GamaActivity due to memory needs, and then recreates it for the onActivityResult. This is part of the normal Android Activity Lifecycle. Now when that happens, gameData will be null, and onCreate is called to init. onCreate will call again the Room function to fill gameData, but as Room is asynchronous, it will let onCreate finish, before the gameData is filled. At that point, onActivityResult will be called, and gameData may still empty, as the the Room code hasn't finished, so app crashes.
      The correct way to fix this, is to move gameData out of the Activity class, and to the Application class. That is where it belongs, as data should be tied to the Application, not the Activity. You will have to extend the Application class, and in the onCreate of the Application, init the Room data. Just Google "Extending Android Application Class" for specific instructions. This way the data will be safe for the lifetime of the application.






      share|improve this answer




























        0












        0








        0







        I suspect the problem happens when the OS destroys the GamaActivity due to memory needs, and then recreates it for the onActivityResult. This is part of the normal Android Activity Lifecycle. Now when that happens, gameData will be null, and onCreate is called to init. onCreate will call again the Room function to fill gameData, but as Room is asynchronous, it will let onCreate finish, before the gameData is filled. At that point, onActivityResult will be called, and gameData may still empty, as the the Room code hasn't finished, so app crashes.
        The correct way to fix this, is to move gameData out of the Activity class, and to the Application class. That is where it belongs, as data should be tied to the Application, not the Activity. You will have to extend the Application class, and in the onCreate of the Application, init the Room data. Just Google "Extending Android Application Class" for specific instructions. This way the data will be safe for the lifetime of the application.






        share|improve this answer















        I suspect the problem happens when the OS destroys the GamaActivity due to memory needs, and then recreates it for the onActivityResult. This is part of the normal Android Activity Lifecycle. Now when that happens, gameData will be null, and onCreate is called to init. onCreate will call again the Room function to fill gameData, but as Room is asynchronous, it will let onCreate finish, before the gameData is filled. At that point, onActivityResult will be called, and gameData may still empty, as the the Room code hasn't finished, so app crashes.
        The correct way to fix this, is to move gameData out of the Activity class, and to the Application class. That is where it belongs, as data should be tied to the Application, not the Activity. You will have to extend the Application class, and in the onCreate of the Application, init the Room data. Just Google "Extending Android Application Class" for specific instructions. This way the data will be safe for the lifetime of the application.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 25 '18 at 17:51

























        answered Nov 25 '18 at 5:29









        lionscribelionscribe

        2,0381613




        2,0381613






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53464512%2fcache-is-empty-after-the-activity-is-returned%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Contact image not getting when fetch all contact list from iPhone by CNContact

            count number of partitions of a set with n elements into k subsets

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks