Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Android: How to deal with App being closed in the background?

  1. #1
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Android: How to deal with App being closed in the background?

    So I've come across something with my Android app which I am apparently not handling well (or at all).
    If I open my app, then press the home button (so the app is now running in the background), then wait a long time (perhaps do other things in the meantime), and then open the app again, I get weird behavior. Specifically, the app remembers what activity it was on, but it doesn't remember much else about that activity.
    In my scenario, I have a puzzle game which is drawn onto the activity. When I start up the app again at that later time, the activity for interacting with the puzzle opens (as if the app was just put on hold), but the puzzle itself is gone. This only happens after a long period of time running the game in the background.
    Furthermore, my app is not listed on my device as an "Active App" when this happens, which leads me to believe Android has closed the app due to inactivity, but saved the app's state or something (since reopening the app doesn't start the launcher activity). I suppose the other possibility is that the puzzle data was garbage collected.

    So my question is: what would be the "correct" way of preserving the puzzle data? My initial assumption would be saving it in a temp file, but I'm not sure when I should tell my app to create that temp file, or if this is even a valid assumption.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Android: How to deal with App being closed in the background?

    Record the state of the app in the onPause() function, and reinitialize it to that state in the onResume() function.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Android: How to deal with App being closed in the background?

    Chances are your app is being destroyed by the OS either because it is in the background for too long or another process needs the resources. The correct way of dealing with this is to re-create the activites states with a Bundle. Override the activites onSaveInstanceState(Bundle savedInstanceState) and create key-value pairs that define the state of the puzzle. You've probably seen it thousands of times before but the activities onCreate(Bundle savedInstanceState) comes with a Bundle that can be used to re-create the activity.

    It's a much cleaner approach than creating temporary files but it also has a few un-expected benefits. For example, every time the screen is rotated an activity is destroyed and re-created. Now, android automatically stores the state of all objects that extends a View which is why this process is typically transparent to a developer but in your case the Puzzle activity has some state that isn't captured so using a Bundle will let you re-create the activity when the screen is rotated or the user hits the back button.
    Computers are fascinating machines, but they're mostly a reflection of the people using them.
    -- Jeff Atwood

  4. #4
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Android: How to deal with App being closed in the background?

    That's an interesting solution that seems to be what I want. But there are a few things I'm curious about.

    Ok, so I have a separate class for the Puzzle which is in a library that is independent from the Android app (the library has no reference to anything Android related). The specific Puzzle object that the App is using is stored in a static Data class, which just contains the puzzle and a handful of constants which wouldn't have made sense to put in the resource folder or any specific class. The activity in question refers to the static Puzzle object in the Data class. Because of this, the Puzzle object itself is not effected by the screen being rotated or the user pressing the back button or anything (because recreating the activity doesn't recreate the Puzzle object).
    The Puzzle class itself contains lists, multidimensional arrays of objects, ect., so it's not just a bunch of raw data. While looking over the Bundle object, it seemed that you can only store raw data and Parcelable objects. The Puzzle object obviously contains a heck of a lot more than just raw data (I have to dig pretty deep into the infrastructure before I get to raw data), and since the Puzzle object is in an Android-independent library, telling it to implement the Parcelable interface would not be an option.
    I could probably create a wrapper for the Puzzle object which implements Parcelable, but I would then have to create similar wrappers for all the other classes which the Puzzle object contains, which seems like major overkill to solve just this problem.

    Do you know if there is a way around this problem for me to use the solution you suggested, or should I probably just stick to the temp files? The size of saving the puzzle isn't enormous, perhaps at most 100kbs. Furthermore, if you do suggest I stay with the temp file, should that file be created/read on the onSaveInstanceState()/onRestoreInstanceState() methods, or the pause()/resume() methods? I want to reduce the number of unnecessary reads and writes to files if possible.
    Last edited by aussiemcgr; August 21st, 2014 at 10:10 AM.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

  5. #5
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Android: How to deal with App being closed in the background?

    You re-create the Views with a Bundle but your model sounds complex enough to warrant it's own serialization. Kevin's answer is the way to go; serialize in onPause() and deserialize in onResume()
    Computers are fascinating machines, but they're mostly a reflection of the people using them.
    -- Jeff Atwood

Similar Threads

  1. Creating an android app.
    By absineo in forum Android Development
    Replies: 3
    Last Post: May 13th, 2014, 05:02 PM
  2. QA for Android app
    By deependeroracle in forum Android Development
    Replies: 1
    Last Post: February 28th, 2014, 10:23 AM
  3. Creating an android app.
    By absineo in forum Java Theory & Questions
    Replies: 1
    Last Post: February 4th, 2014, 09:17 AM
  4. [SOLVED] Wanting to prevent my app from being closed using Task Manager
    By mds1256 in forum Java Theory & Questions
    Replies: 5
    Last Post: September 30th, 2013, 02:23 PM
  5. Hello World Android App
    By TP-Oreilly in forum Android Development
    Replies: 6
    Last Post: January 22nd, 2012, 08:01 PM