Currently I am working on a application, which contains one switch. When we touch switch , it initiates a Dialog.
I want to know:
Since My Dialog box is above Activity, when we exit from dialog why onResume us not called.
Which functions get called when Dialog ends
You will need to implement the onCancelled() and onDismiss() listeners for the Dialog. These will be called when the user either cancels, or dismisses the dialog.
The reason onResume() is never called, is because the activity is never paused. Its just an overlay, and your activity carries on in the background.
Related
I have an alert dialog in my activity. When the user presses the home button when the dialog is dismissed, i would like to have it re-open when the activity resumes.
Right now, i am dismissing the alert onPause() event of the activity.
How can i do this?
probably you could have a variable that indicates that the alert dialog is showing.
bool isAlertShown=false;
set it to true when the dialog is shown.
set it to false when the dialog is dismissed.
onResume event of your activity, check if the variable is true, if yes, you should be showing the dialog.
Show dialogs with DialogFragment, it handles state automatically.
if you want to show every time when your activity resumes show it in your activity's onResume()
if you want to show dialog in case dialog was showing before user pressed home declare a variable such as "isDialogShown" and set it true or false. Do not forget to save your flag variable onSavedInstanceState()
I advice to use Dialog Fragments (http://android-developers.blogspot.com.tr/2012/05/using-dialogfragments.html) for these kind of operations.
In my application I want to handle the state if my displayed dialog looses focus(e.g. if my dialog is show and any system dialog(like low battery dialog or Power button dialog) appears on top of that).
Is there any way to capture this scenario? I did not find any method for dialog having onWindowFocusChanged method.
My activity's onWindowFocusChanged does not work here.
If you use a DialogFragment when the activity goes to the background (or something comes in foreground of your app) the dialog onPause will be called, then upon returning to your app/activity/dialog, the onResume will be called.
Always use DialogFragment, the Activity dialog lifecycle methods are depreciated for a reason.
Examples of how to use DialogFragment can be found in the docs here.
I have an activity which, when started, needs to check if the user is authenticated. If not, I need to display an interface to authenticate. I do this with another activity, which has a dialog theme, and I start it in onResume() with flags NO_HISTORY and EXCLUDE_FROM_RECENTS.
Everything works fine when starting the application for the first time. But I have a feature that resets login after some time, if the user is not in an activity. When I test this, I start the applicatio, enter the password, then move back to home. Then when I enter the application again, the background darkens as if the dialog would show, but it doesn't.
At this point, if I press the back button, the darkening from the background activity disappears for a second, then the dialog finally appears.
I used logcat to investigate the case, and the activity lifecycle functions get called properly:
//For the first start:
onCreate background activity
onStart background activity
onResume background activity
onPause background activity
onCreate dialog
onStart dialog
onResume dialog
//Enter password
onPause dialog
onResume background activity
onStop dialog
onDestroy dialog
//navigating to homescreen
onPause background activity
onStop background activity
//starting again
onRestart background activity
onStart background activity
onResume background activity
onPause background activity
onCreate dialog
onStart dialog
onResume dialog
//no dialog shown, only darkened background activity recieving no input
//pressing back button
onPause dialog
onResume background activity
onPause background activity
onCreate NEW dialog
onStart NEW dialog
onResume NEW dialog
onStop OLD dialog
onDestroy OLD dialog
//now the dialog is properly shown
//entering password
onPause NEW dialog
onResume background activity
onStop NEW dialog
onDestroy NEW dialog
Using the SINGLE_TOP flag makes no change. However, if I remove the dialog theme from the dialog activity, it IS shown after the restart.
So far I didn't want to use a Dialog instead of an Activity, because I consider them problematic sometimes and less encapsulated and this part has to be quite secure. You may be able to convince me though..
Thank you in advance!
What happen if showDialog of activity get called ?
Does activity call onPause if yes why ? As it is the part of Activity only then why onPause gets call?
onPause() isn't called on activity that showed dialog.
onPause() is called when your activity is no longer at the top of the activity stack. A Dialog by itself is not an Activity, so will not replace the current Activity at the top of the stack, so will not cause anything to pause.
A dialog (lower-case) does not need to be implemented by a Dialog class, however. For example it is not uncommon to implement one with an Activity whose theme is set to that of a dialog. In this case displaying the dialog-as-an-Activity will cause the new Activity to be on the top of the stack, pausing what previously was there.
See this answer
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()