I have an quiz app. and i want the user to choose the type of questions from alert dialog . so the alert dialog has a check boxes buttons so if the user can select more then on type. when the user click the positive button in alert dialog it will send info to if condition then the condition will filter the questions and then will pass it to an array. the problem is the array its inside oncreate and i want the alert dialog to be in same class so when i run the class oncreate will start and i get null exception because i didn't pass any info to the array. How can i delay oncreate() and make it load only after the user click on positive button of alert dialog. The alert dialog will be in same class . So how can i do it ? is it possible? and thanks.
-
More explanation here would help a lot... – PearsonArtPhoto Nov 29 '12 at 16:06
-
You can't delay onCreate() nor would you **ever** want to do so. Simply pop-up the AlertDialog in onResume() and don't let people leave it until they close it by pressing "ok". – DeeV Nov 29 '12 at 16:08
-
I have an quiz app. and i want the user to choose the type of questions from alert dialog . so the alert dialog has a check boxes buttons so if the user can select more then on type. when the user click the positive button in alert dialog it will send info to if condition then the condition will filter the questions and then will pass it to an array. the problem is the array its inside oncreate and i want the alert dialog to be in same class so when i run the class oncreate will start and i get null exception because i didn't pass any info to the array. so how can if fix it? – Taha Nov 29 '12 at 16:14
4 Answers
onCreate() is the first called when the activity is created per the android activity lifecycle so, no. I'm not sure why you would want it in the same activity if you don't want it to start unless they click 'OK'. Put it in it's own class or the previous activity.

- 44,549
- 13
- 77
- 93
No you cannot, if you attempt to delay something within the OnCreate() method within an activity it will throw an exception as you don't have a context to place the alert dialog in at that time. As codeMagic previously stated, if you have a prior activity before this one, your best bet would be to create the alert prior to calling startActivity on that intent.

- 399
- 4
- 5
I would suggest to use Fragments in this case. Load Activty, show dialog and based on the answer you will display "correct answer fragment" or "invalid answer fragment". With this solution activity is loaded (no need to delay) and you just dynamically change the content of the activity.

- 91
- 2
Try using the AsyncTask class. The onPostExecute method may do the job you're looking for ..

- 242
- 2
- 4