AlertDialog lost when new Activity begins - android

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.

Related

android alertdialog with broadcast receiver

I have a network monitor (using Broadcast receiver) in my app, what it does it pops an alertdialog if the phone is not connected to internet and if the phone connects back it pops another alert informing the user he is back online. The problem is every time the monitor sees a state change it creates another window that stacks on top of the other one. For example if the phone has its DATA ON/OFF 3 times it will pop 6 messages. Is there a way of dismissing an alert dialog if another one opens or what would be the best approach to overcome this scenario?
Thank you in advance, your help is very much appreciated.
Android newbie
Add a variable containing the currently visible dialog:
Alertdialog a;
and then when you want to show one:
if (a!=null) {
a.cancel();
}
a = yourDialogBuilder.show();
Hope this helps.
Dialog has an isShowing() method that should return if the dialog is currently visible. So you can use that to see if a dialog is showing and hide it with dismissDialog().
I think you should consider re-designing your notification mechanism. Using Dialog can be very disturbing to the user experience since it requires an action to dismiss and interrupt the natural flow of the app.
Consider using Toast for simple notifications or Notification if you want it to show in the statusbar. Also, you can have some icon in your application that reflects the current connectivity status e.g. green is connected and red otherwise.
To ensure only one dialog is available, you can create a static instance of your dialog in your activity and dismiss it if it is not null before showing a new dialog

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.

Avoiding Blank Screen when it Raised from Intent in Android

I have an application that raises from service on scheduled time. In that service, i have displayed my Activity using Intent. It works perfectly. But, when i go back, the activity is finished. But, one blank screen is remaining there? How can i avoid this blank screen. I need my application's Activity instead of blank screen. Anyone Guide me.
#Špĸ remove this line: i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
take a look at my post if you wish to open a dialog via your service: Creating global dialogs
I don't like that you finish an activity that you go back to. You have multiple of different alternatives:
Using fragments instead of activity intents.
Set flag to indicate the activity is done so it will close when you go back.
You could also try to work with the stack, look at this: https://stackoverflow.com/a/4038637/969325
Without seeing the code it is very diffcult to find out what is going wrong in your code but still I am making some effort to help you.
Add android:theme="#android:style/Theme.Translucent" to your activity tag in your manifest file and then try if it works or not.
Have you tried setting FLAG_ACTIVITY_NO_HISTORY in the intent you use to start the AlertDialog activity or launchMode="noHistory" in the manifest.
The AlertDialog activity may very well be the blank screen you're seeing as no content view is set and the only thing a user sees is an alert dialog so once the alert dialog is dismissed and the app goes back to this screen it would be blank.
Before press back button, you have to save the state of the activity and restore the activity while come front. This is easily done by onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() method. Please refer this link.

Dialog is re-presented after onPause()->removeDialog() - why?

I'm just not getting why this is happening:
Successfully create dialog and instantiate it when user clicks on an item. Zero problems getting dialog to show when desired and to go away when back is pressed....
The problem is:
If the dialog is still displaying when the application/activity goes away (pause, die), when it returns, the dialog is still presented but no variables which are context specific are presented. Note: I do issue removeDialog() for the dialog in the onPause(). Yet mystically when the app returns, it's somehow cycled thru onPrepareDialog() and loaded up with missing variables (xml names show).
What I want is for the dialog to go away and let the user make a different choice from the main activity and then redisplay the dialog - if that's what they want. Or said another way - I want dialog to always go away if the app goes away.
I've looked into persisting the data but there doesn't seem to be a "right" spot in the restore process to do that (or I'm just ignorant). I can't figure out why onPrepareDialog is getting invoked during onResume()....

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