Halting an activity until I get result from an AlertDialog - android

Here is my problem - I want to create an activity and a dialog (with text field and OK button). I want to do the following:
Show the AlertDialog (on creation of the activity, on clicking a button, or some other actions);
Fill the text in the AlertDialog, and click the OK button;
Continue doing the main thread of the activity;
Something like this:
public String getText() {
String result = null;
// Showing the new window with the text box and the button, and after
// the button is clicked to move to the return statement below;
return result;
}
I tried using "runOnUiThread" and "AsyncTask", but the actions on the field "result" are done only in the "protected void onPostExecute(Void result)" method, and meanwhile the main program is still executing, without waiting for my input.
Is there anyway this to be done (I am sure it has, because saw such apps) to solve this problem? I know that is really impudent to ask for such help - but is is possible to write some code example, just to see how it is really happening, because more than 3 days, I can' do it. If not please give some suggestions, and will continue trying and trying :)
THANKS A LOT!

Whatever the operations u want to perform after click ,write in asynctask .When dialog button clicked execute the asynctask.Thats it

Well that's pretty much how dialogs work anyway.
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
At the bottom it shows how to create a custom dialog.
However, a lot of the time the text you need to take in is also displayed on the activity anyway, could you not embed an EditText on your activity and simplify this task?

Related

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

Method call terminates in between and jumps to the next method call

I'm trying to build a notepad application in android. I'm in this situation, when the user chooses to create a new file while still working on a file, the application should first ask for a save and then restart the MainActivity to create the new file.
So, basically, the sequence is as follows:
->Working on current file
->Clicks on New File
->Save dialog
->Restart MainActivity.
For this, I've been using a method called new file as below:
public void new_file()
{
save_file();
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
The problem here is that, everytime I run the application and text this part of the code, the dialog pops up and immediately a new file.
I would like to know how I can first let the Activity finish the save_file() method call and then the Activity Restarts.
Thanks in Advance
It looks like it works fine, but not seeing safe_file() code (BTW: consider switching to more standard function naming convention) it's hard to be sure , but note dialogs are asynchronous, so you cannot start dialog and expect all further code to wait for dialog dismiss. I assume you popup dialog in safe_file() method, so you should either call back to start new activity from there when done saving or you should do that in your dialog click listener.
Use the Negative, Positive and Neutral buttons of the AlertDialog mechanism to detect when a user has exited the dialog. Then, use an OnClickListener to decide which button was clicked, and only restart the Activity then. Something like:
Show dialog
Wait for one of the buttons to be clicked
Restart activity when a button is clicked, as that means the user is exiting the dialog.

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?

Android, Limit same button pressed multiple times

I have buttons in my app that do various things. A problem i am running into is that a button can be pressed multiple times before what that button does is enabled/processed/calculated.
Example
when i press a button { an integer should have 1 added to it unless it is at it maximum value.
when i press a button { a dialog should show (only once).
in either case if i press slowly i have no problems, but if i press rapidly the integer will pass its maximum or multiple dialogs will show.
Can someone point me in the right direction to deal with this.
It sounds as though you could simply set a boolean value when the button handler is entered to signify that the task is being performed. If you enter the function and the value is already true then simply return (or just disable the button until the operation is complete).
I don't know a lot about android, and I don't know if you have the task running in a different thread or if multiple clicks are being queued up, in which case the boolean wouldn't help as it would all be happening in serial. In that case, as I suggested earlier, just disable the button while the task is in progress (probably a good idea in any case).
I would suggest disabling is not that user friendly, i see the user is selecting button and has a lot of time in between before the next operation happens and tats why he ends up clicking multiple times. In this case as soon as soon capture the click you can show circular progress dialog , that shows user something is in happening and he wont be able to click button also.
Dismiss the dialog once you have set the counter
or second case about to open the dialog ProgressDialog dialog = ProgressDialog.show(MyActivity.this, "",
"Loading. Please wait...", true); then dialog.dismiss() once ur done

Application crashes when user presses Back Button

Good day all, First of all, maybe i went about this the wrong way in the first place. Situation is, i have a listview that when an item is clicked, it creates an intent and calls startActivityForResult() to another activity, to be able to edit the entries in the row. and that other activity after editing the required values, returns back to the calling activity, when the user clicks on a save button. The activity then uses the row Id to update the item.My problem now is that, if the user presses the BACK button instead, the application crashes. i have been looking around and see solutions like using Shared Preferences or onSavedInstanceState(), but i don't know where exactly i should be putting the code? Any help as usual, will be greatly appreciated. Thank you.
p.s: when i look at logcat, its gives a NullPointerException for this line in onActivityResult.
Bundle result = data.getExtras();
If you are assuming you will have a result on the parent thread if the user quits the activity via Back then that is most likely the issue. Try testing if result is null before attempting to use it.

Categories

Resources