Avoid Loading Data Again When Finishing Activity
Solution 1:
Define a static variable in your main activity class like this:
publicstaticbool download = true;
When you are returning from settings change its value to false like:
MainActivity.download = false;
And put your download method inside an if statement like this:
if (download) {
yourdownloadcode();
}
Solution 2:
That is the usual behavior of Android. When you launch your "Settings Activity" your "Main Activity: calls onStop and the Activity is stopped. Now when you click the back button from the "Settings Activity" the "Main Activity" comes to the forefront and the onStart method is called again. Since you say that you are downloading the data in the onStart method, the data will again get downloaded.
You can avoid that by downloading the data in the onCreate Method.
Solution 3:
When your main activity become visible after you press Back
button on your Settings activity, onStart
method is called once again (see Activity Lifecycle). It is why you start re-downloading data once again. So, keep some flag which indicates, that you already started download task. But keep in mind that your main activity can be killed any time after its onPause
method get called.
Solution 4:
If you want to load data only ones you can use Activity onCreate
. Here is the activity lifecicle information: http://developer.android.com/reference/android/app/Activity.html
startActivityForResult
you can use like a callback for your settings screen - if you need to change Main Activity layout after you change configuration on your Settings screen. You can find an example here http://saigeethamn.blogspot.com/2009/08/android-developer-tutorial-for_31.html
Post a Comment for "Avoid Loading Data Again When Finishing Activity"