How to delay an activity until user accept alert dialog - android

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.

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.

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.

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.

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

Related

Perform a button click when activity opens

I have an activity which has a button which opens up a custom AlertDialog (in which user can enter some data) when pressed. I now have a requirement to open this activity with the alert dialog open initially.
I know that I can perform a click on the button programmatically by calling button.performClick();. My question is, when should I call this? Is it safe to call it in onCreate()?
Yes , you can call it in onCreate() , if you want to call it only once,
but if want call that again you leave and come back to Activity it will be better to add in onResume().
Yes, you can call it in onCreate(), but if you set the AlertDialog as a login, I think it will be in onStart(). Because when you go to desktop, then if you want to get back, you should login again, right? And you can read the detailed information in this.

How to catch touch event of a dialog when it is clicked outside

I have Dialog in my activity, what I want is suppose the dialog is open, then on Touch of outside dialog I want to dismiss the dialog, and at the same time, I want to call a function which does some update in my activity.
Initially I used MyDialog.setCanceledOnTouchOutside(true); But these will only dismiss and in my case, at the same time I want to call some functions whenever user click outside of a dialog. So what should I do? I know that if I can extend the Dialog class and override its onTouchEvent method then it will be solved but in my case, my class already extends Activity class and in java, we can't extend more then one class.
So what will be the best solution for that? Please help me to solve this out.
The second answer on this thread shows how to do this (used it myself, works):
How to cancel an Dialog themed like Activity when touched outside the window?

how to make an alert box display over other activities

Suppose i am having an activity (let's say activity1) and i have given a command for doing some long process. Mean while i am starting another activity (activity2), during this time if the activity1 finishes the process and shows the result in an alert box, then how can i make this alert box of activity1 appear over activity2? What i have noticed is that, the alert box of activity1 is only visible when i move back to activity1. Is there any way to do this? Ignore if the question is irrelevant, as i am just a beginner in android.
Before using two activity, you have to save the state of the activity and restore the another activity while come front. This is easily done by onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() method. Please refer this link.

Get a open Dialog without having any references to it

On Android, if I added a Dialog into the screen and another Activity opens, how can I get from that Activity all the open Dialogs?
I need to close one of them.
You should create a class and extend DialogFragment and in onCreateDialogreturn your dialog, then show this dialog fragment with a specific tag , after that , you can find this dialog by using activity.getFragmentManager().findFragmentByTag("the tag").
Don't hold a reference to your dialog (local or global) because the activity can get recreated and it will leak the reference in the memory, just get the reference with the findFragment method.
If you are in a fragment , you can use fragment.getChildFragmentManager(...) and do the same.
Good luck!
When another activity opens, your current activity will go through the onPause() state. More info on it can be found here.
In the onPause() call dialog.dismiss() and you should be set.

Android - entries in dialog should be deleted when dialog is closed

I have a customDialog with input fields. I want possible entries to be removed once the dialog is closed (either via back or when a certain button is pressed), i.e. the state should not be saved.
How can I do that?
If the back button is pressed means that dialog is canceled. Implement DialogInterface.OnCancelListener for your dialog and empty/delete/null the entries you want.
I think you might be getting another problem.
Say that you have shown a dialog which was dismissed. If the same dialog is going to be shown a second time, it won't be rebuilt. It will just be shown again.
This means that if you setup your dialog in the onCreateDialog method, the second time that the dialog is shown, this method isn't invoked! Instead, onPrepareDialog is invoked.
Alternatives? You can call Activity.removeDialog or take care of the setup process in the onPrepareDialog hook.

Categories

Resources