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

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.

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.

Can I get an AlertDialog from the onPostExecute() displayed in another activity?

When my app gets started it executes an AsyncTask, which checks a page, for a never version of this app. If so, it displays an AlertDialog. But right after the UpdateCheck gets executed, the MainActivity starts a new Intent and a new Activity and so the Alertdialog of the AsyncTask won't get displayed until I return to the MainActivity. Is there a way around to show it independend of the Activity or something like a global Dialog? Thanks in advance.

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

AlertDialog lost when new Activity begins

I have an AlertDialog appear whenever I find that the device location has changed. This happens pretty much at anytime the application sees fit.
The problem is that if the dialog appears just before a new Activity is started the dialog disappears with the old Activity. Is there anyway to have the AlertDialog block new Intents? Or translate over to the new Activity (could I update the Context)?
I noticed that AlertDialogs don't even handle the case where the device rotates, so my hope for this isn't too high, but any input would help.
I would simply use a SharedPreference setting and check it whenever your potentially interruptive Activity starts.
Ex;
set shared preference value to 1
start dialogue
when finished, save the shared preference to 0
if activity starts and shared preference is 1, it interrupted so start an alert dialogue. if it is 0, move on.
Again, an AlertDialog is meant to attach itself to an activity, so you really can't stop it from dieing, if your previous activity is pushed back or loses focus of the screen (ui thread basically put on hold!)
Hope you can fix your problem with this minor fixup.
To add what Daniel commented, Custom toasts can be awesome. They are, however, limited to about 3.5 seconds (I think)
More on Custom Toasts; http://developer.android.com/guide/topics/ui/notifiers/toasts.html#CustomToastView
Your question is not clear for me,As I understood you need to show a Alertdialog whenever the device location is changed that too above a new activity.My suggestion is that when ever the device location changes get the need values,bundle it with the intent and pass it starting a new activity.With the bundle also pass a boolean flag.If the boolean flag is true in the new activity show a Alertdialog box in onCreate,I hope you will get what you wanted.This will be the best scenario for you to do this as I understood.Also keeping the history of these activity's may affect the app while implementing this.

AlertDialog disappears after switching activity

I have an android application having an AlertDialog with OK and Cancel buttons. When the dialog shows without pressing the OK or Cancel button, just press Home button of device. The Home screen will show. Now open another application suppose Camera. Take some picture or Video. Now get out from the Camera application. Now open my android application and surprisingly the alertdialog have disappeared. Why?
I'm guessing you are creating this AlertDialog onCreate() method.
First, you should read up on the Activity Lifecycle.
And what happens is that when you go to another app, the Activity goes to onPause method, which cleans up a bit.
Called when the system is about to start resuming a previous activity. This is typically used to commit unsaved changes to persistent data, stop animations and other things that may be consuming CPU, etc. Implementations of this method must be very quick because the next activity will not be resumed until this method returns.
Then because you return to the app, it calls the onResume method, which doesn't create your dialog again.
If you want to show dialog on startup of application then write this code in
onResume()
method, it will show dialog every time when user returns to this screen.
Or you can manage its state in
onPause()

Categories

Resources