Question

My Problem: Is it possible to prevent an activity to call OnResume() when it is being created? As I saw after the OnCreate() and onStart() method runs, the next one is the onResume(), although I only want to have it when I resume the activity from the paused state.

Why do I need this: I launch my activity (FragmentActivity, so lets say OnPostResume() ) starting with a thread which takes about 2-3s to be ready getting data from an external database. After the thread is done, I call a method which needs these data and I want to call it everytime that activity gets visible. The thread runs only when the FragmentActivity is created (onCreate()), and I cannot put the method into the onResume() because onResume() would be running way before the thread would finish its task. So it would receive not-ready data.

Anyone has a better idea?

Was it helpful?

Solution

Not sure of the exact application of this but I'll make a suggestion.

If you use an AsyncTask, you can send it off to get the data you need and in the onPostExcecute() method you can call your method that requires the data or update the view as needed. (It runs on the UI thread)

If you happen to already have the data you need in certain scenarios you could also bypass the AsyncTask and directly update the view.

This AsyncTask can be triggered in the onResume() method.

If I'm missing something, please let me know and I can adjust my suggestion.

OTHER TIPS

I didn't understand the purpose of this, but here's a possible solution:

If you only wish to get the even of onResume on states that didn't have the onCreate before, just use a flag.

In the onCreate, set it to true, in the onResume check the flag (and also set it to false). if it was true, it means the onCreate was called before.

I personally would prefer to check if the result available, rather than always executing the getter-code in onResume. If the user somehow resumes your activity before the background thread is finished, you'd have a call on onResume, but don't want to display a result.

Maybe it would be a good idea to calculate/fetch the values in the thread, and let the thread return immediately (and cause the values to get filled in) if the values are already cached somewhere. That way you'd only have one entry point (the thread) for updating your UI instead of two (the thread and the onResume method).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top