1

I got a fragment, that should create some files, iterate through the contactbook and some other pretty long tasks. The fragment is a pretty simple "Hey, please wait" fragment with a label and a progressbar. I used factory pattern to pass arguments to this fragment.

My fragments code is comparable to the solution of this question, only with other parameters and members. In the onStart-method I want to do my long tasks, but strangely it starts the code before I see my view, which should be created by the onCreateView-method first, if I remember the Fragment-lifecycle correct.

Is this a side effect of Factory Pattern or what am I missing here? What would be the best solution here, so that the view is actually shown before he starts my long code execution?

Community
  • 1
  • 1
Tristus
  • 183
  • 1
  • 2
  • 15
  • Is the app coming back from background? Then you only get onStart() and no new onCreateView() – mach Feb 20 '15 at 11:10
  • no, this happens also when its not in the background. I also get the view at some point, but it seems to start like 1 second after onStart. – Tristus Feb 20 '15 at 11:12
  • Start the task in `onCreate()`. Don't forget to call `setRetainInstance(true)`. Then `onCreate()` will be called only once, but `onCreateView` will still be called as needed. – Eugen Pechanec Feb 20 '15 at 11:15

2 Answers2

1

OnStart is called when the activity is becoming visible to the user. onCreateView will create your view but its not visible yet. You should start your long operation in onResume.

Nauman Afzaal
  • 1,046
  • 12
  • 20
0

Please refer to http://developer.android.com/reference/android/app/Activity.html#onResume()

Basically as Nauman mentioned, often an indication that your activity is ready to be interacted by the user is the onResume. but ideally, use

onWindowFocusChanged(boolean hasFocus)

but keep in mind that this is not part of the normal lifecycle event flow. This method can be also called when dialogs come up in front of the Activity etc

see http://developer.android.com/reference/android/app/Activity.html#onWindowFocusChanged(boolean)

harrane
  • 959
  • 11
  • 24