Displaying a AlertDialog on screen irrespective of Activity Screen - android

I want to show an Dialog Whatever the screen the User is in. Suppose if user opens application and in initial screen if I receive a server message I have to show it in a dialog.Meanwhile there is an option of autologin . So it could move to my next activity.If this is the case that dialog should not be closed.It should show on newly opened activity rather than the previous activity.And other thing is that even though the dialog is shown I should be able to control my buttons on the activity.
Here's what i am doing.
if(Activity1.mcontext!=null){
CommonMethods.showDialog(sliderMessageText,
LoginActivity.mcontext,"activity1");
}
if(Activity2.context!=null){
CommonMethods.showSliderMessageText(sliderMessageText,
Activity2.context,"activity2");
}
if(Activity3.mcontext!=null){
CommonMethods.showSliderMessageText(sliderMessageText,Activity3.context,"activity3");
}
Instead I am displaying the dialog in all the activities.
Thanks in advance.

You may want to consider using an Event driven model such as GreenRobot.
http://greenrobot.org/eventbus/
This would allow you to efficiently handle the scenario you describe.
Alternatively, you can use the LocalBroadcastManager to communicate between different parts of your app.
e.g Send a broadcast message when you want to display a dialog and handle that message in all of your Activities

Related

how did facebook messenger switch between activities

how can facebook hide current activity and show another activity here in chathead
when choose another one to talk with him its hide current popup floating activity and show the other one activity
i don't think its start new activity because i write some message on current activity in the editText without send it and when choose another one to talk with him its hide current activity and when i back open it the text i write it it's still there thats mean it's dosen't start new activity when switch to other conversation
iam try to make something like facebook chathead but i have problem i switch between floating activities
They are using Fragments and Floating Action Buttons.
Also just for your information, unless you specifically call finish() on an activity, it will only get stopped when the new activity is started. When you return, it will go back to onResume() state.
Please look into activity life cycle and Fragment Life cycle to know about these things more clearly. The faster you have a clear idea about these, the easier it will be in the long run
https://developer.android.com/training/basics/activity-lifecycle/index.html
https://developer.android.com/guide/components/fragments.html
NOTE: When it is on the home screen, It is an window running on top of the background service with a floating window , while inside the app it is a Fragment.
see tutorial: http://androidsrc.net/facebook-chat-like-floating-chat-heads/

Is it possible in android to leave an AlertDialog in front of changing Activities?

I have an app with multiple Activity classes. In one part, one is a main screen and the next is a login. The user would open the login screen and enter their info and press a button to submit it. The resulting code opens up an AlertDialog to tell them the registration status, along with some other information. If the registration itself was successful, they will return to the previous activity. But this makes any AlertDialog disappear with it.
I want the background to return to the previous activity so they can see the home page start updating, but I want them to see the alert message long enough to read it.
Yes this is possble. By using a service you can display an alertbox which runs in foreground for a long time. I have done thissame kind of thing for a news app.
It will run in foreground for a long time and when you want to switch you can stop the service than it will dismiss the alert box-.
In my case I displayed a custom view with buttons in the foreground.

Maintain Single instance of an activity

i have button in my first activity called Start.
Now when i click on this button it takes 1 to 2 seconds to load the next activity, now at that time the user clicks on the start button multiple times so what happens is that the next activity will open multiple times.
How to overcome this? Is there any way that even if the user clicks on the Start button multiple times open the activity only once.
Your Options:
On click, disable the button and display a ProgressDialog to the user.
Use the Intent flag FLAG_ACTIVITY_SINGLE_TOP to ensure only one activity is maintained on the stack. Documentation
Use the qualifer launchMode=singleInstance in your AndroidManifest.xml so only one instance of your Activity is allowed at a time. Documentation
I would recommend the first, because it can show the user your application is still working even if it takes a few seconds to do the necessary processing to begin your Activity.
You can put the launch mode of your 2nd activity as "Single Instance" in your manifest file.
Don't use anything like a launchMode or Intent flags. They are used for different purposes.
Description here
What you have to do is:
Show a progress dialog to clearly show the user that an
action(calling 2nd Activity) is in progress. This was user will not
try to click the button multiple times
Simply disable the button's click listener after 1st click. This is not
recommended because user might not be able to know whether he/she
clicked the button. Also this is the case where user tends to click
the button multiple times.

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

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.

Categories

Resources