views:

66

answers:

2

I'm writing a bitmap editor and I'm trying to write an autosave feature. When onPause is called, I write the application state to an autosave file. As this takes between 0.1s and 1.5s, I've been advised this IO operation be performed in a background thread.

In the onCreate method of my activity, I check to see if the autosave file exists and, if it does, I open it.

Are there any scenarios I have to consider where the user can somehow leave the activity, the autosave thread starts and the user can return to the activity before the thread has finished? If so, how can I detect this and wait for the thread to finish before I check the state of the autosave file?

I was going to make it that, when the user backs out of my activity, they're asked to wait a second while the data is saved. This seems OK, but I can't do this when my activity is interrupted by something like a phone call.

Also, I'm a bit confused about how multiple versions of the same activity can be started as this makes dealing with autosaving more complex. Is there a way to make sure only one instance of my activity is allowed to run at a time?

+1  A: 

I would suggest you use a Service class to handle the save and load of such states. A Service should be guaranteed to be singleton thru out the application life-cycle.

xandy
If you are asking then this should be a comment. Please delete this.
the_drow
A: 

I was struggling with similar problem, but in my case there was a thread which controls idle period for application and autoclose if idle expires given period. I have found that 1) There can be multiple instances of activities (including main one) 2) Thread has to be singleton or instantiated in child of Application (as of Activity.getApplication()) 3) In order to keep multiple instances of activities my thread was keeping in memoty list of running activities, just adding to list during onCreate() and deleting during onDestroy(), like:

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    getApp().addActivity(this); //add to thread controlled by Application
}

@Override
public void onDestroy()
{
    super.onDestroy();
    getApp().removeActivity(this); //remove from thread controlled by Application
}

Hope this will help...

barmaley