create Dialog over another dialog - android

I want to create a dialog. I create a class and class extend Dialog. I create an object from this class when click on a button from my activity and call show method. If i click on button 2 times or more, second dialog create over first dialog and third dialog create over second dialog and more. when i click on back button , my dialogs remove one by one. How can i solve this problem? Please say a recommend. Thanks

Dialog has a function named isShowing() which returns a boolean to indicate that your Dialog Show status , when your button pressed you can check that if previous Dialog isShowing(), don't instantiate it again .

Related

How to hold the activity when the dialog is open

I am working with Android, and I open a Dialog using myDialog.show(); from the Activity, this Dialog has a Button and when I click that button the Dialog closes without problem using this.hide();.
this is the part of code where I have the question :
myDialog.show();
Toast.makeText(this, "the dialog is closed", Toast.LENGTH_SHORT).show();
the Toast is displayed during myDialog is open, and I thought that when myDialog is open, this hold my Activity and the Toast can not be displayed, but it is not the case.
So what I want is that myDialog once opened hold the Activity and when it is closed the Activity continue to the next instruction which is the Toast
just Override onDismiss in your dialog and put your toast in there. onDismiss gets called when the dialog is closing.
of in your activity implement OnDismissListener and set the listener in your dialog
I think that a similar question is asked here, and the solution that proposed is to create your myDialog.setCancelable(false);
I find this here :
If you want to literally not have the function that brings up the dialog return until the dialog is closed, you're in for some trouble. That's not the way the Android UI works.....

How to delay an activity until user accept alert dialog

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 ..

How a dialog fragment set result to other fragment

I have a fragment that has a button.
When I click on button the system opens a dialog fragment where the user can choose some info.
In the dialog there is a button "Ok". When the user clicks on ok, the dialog dismisses and we return on the other fragment.
I want to return to the other fragment with the result(the info choosen).
How can I do it?
There are several way to achieve this:
You can save the result from the Dialog in the activity (getActivity()) and get in from the other Fragment
You can let the base Fragment implement a callback interface and call its callback function when pressing OK from the Dialog. You'll need to pass the interface (probably it'll be "this") as an argument when creating the Dialog Fragment.
Hope this helps.

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?

Custom Dialog - Login - back button?

I'm trying to setup UI where user will have to login first time they use applications. And Custom Dialog seems like a good thing to use as I want main UI to be kind of visible on background.
So, what I did - I created main Activity and use ShowDialog() with onCreateDialog from main activity.
I created
public class LoginDialog extends Dialog implements View.OnClickListener
and I can control all stuff on dialog just like activity.
Ideally I like to check if user logged in on main activity and if not - show this dialog. Otherwise just go with activity.
On dialog I'd like to log user in and if user clicks back without logging in - I want to detect it and just finish main activity. This is where I have problem.
In WinForms I would do ShowDialog() (in C#) and next line executed when dialog closed for any reason. I can't figure out how to do this in Android.
I didn't get to it yet, but I want to show progress bar when Login button clicked. This bar will be in Dialog box. Is it possible/doable?
Thanks in advance!
You can detect dialog dismissal using setOnDismissListener. In this method you could also call MyActivity.this.finish().
For showing a ProgressBar during the login process, you probably want to look at this answer. It shows the basic structure of AsyncTask and you can adapt it to use ProgressBar instead of ProgressDialog.
You would be changing the bar's visibility in onPreExecute and onPostExecute with bar.setVisibility(View.VISIBLE) and bar.setVisibility(View.INVISIBLE).
Edit
dialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
dialog.dismiss();
if (!isLoggedIn()) {
MyActivity.this.finish();
}
}
});
This code should be in your MyActivity wherever you create the dialog. You need to check to see if the user is logged in or not, because onDismiss will be called whether it's the user or your own code that closes the dialog.

Categories

Resources