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!
Related
I have simple code with transparent activity (it is imposed on my MainActivity). I need to kill this transparent activity by clicking button and after that I need to show dialog. But get some problems first of all if I do that :
public void buttonClick(View view) {
if (view.getId() == R.id.bToK)
{
onDestroy();
}
}
transparent activity it's still visible just nothing happen (in debug mode I see that it goes to onDestroy but I doesn't destroy it at all)
If I change onDestron() to finish() there are other problems because my Main activity became first visible (onResume is called) then called is onDestroy for transparent what provides to next problems during creating this dialog. What should I do If i want show this dialog after kill this transparent ?
Call finish() instead. This will call onDestroy() and respect the activity life. Next if you want open a dialog you can start it before finish or start in the main activity with onResult https://developer.android.com/training/basics/intents/result.html
Simply my problem is that i have the following scenario:
I have activity A ... and there is a custom dialog appearing on it .. it's quite big but it's not an activity it's just a dialog ..
Clicking on a certain item on that dialog opens new activity B
After clicking on the dialog .. I need to dismiss the dialog and open Activity B
what happens is that Activity A appears for like a second or less then Activity B opens ..
I've tried dismissing the dialog after calling startActivity() .. but still the A activity still shows briefly before opening activity B ..
Any suggestions ?
As alternative to #user543 solution (which also valid) you can hold on with dismissal of the dialog. Launch new activity from dialog and leave dialog alone (or better maintain a flag for later dialog dismiss), activity will appear immediately on top of Activity A and it's dialog. When user will click back button, onStart and onResume of Activity A will be called, so check if that flag and dismiss the dialog there
Dismiss the dialog in onStop(). Because after loading Activity B only onStop() will call in Activity A.
Define some boolean variable like:
boolean isClicked=false;
Whenever u r performing some click functionaly in dialog, then make this variable as 'true'. Then in onStop() check like this:
#Override
protected void onStop() {
super.onStop();
if(isClicked)
{
//dismiss dialog
}
}
I have an activity that is themed as a dialog. I have seen that if the dialog is showing, and then I press the home button, and then using the task manager, restart the app, that dialog activity will be the activity that the app starts in, with no other activities available to go back to. That is, the activity that was running when I loaded the dialog activity is not running. So I just have this dialog-themed activity hovering over the desktop. That makes sense.
Looking over the Android activity lifecycle, the OS does remember the last activity and attempts to restart there. So I created all of the on* methods in my activity (onResume, onRestart, etc). What I found was really puzzling. When I restart the app from the task manager, the following methods are called:
onCreate()
onResume()
onStop()
onDestroy()
Where I was really just expecting
onRestart()
onCreate()
onResume()
Why are onStop and onDestroy getting called right away? And why does the dialog still show, even though onDestroy is called?
How can I configure this app so that it never starts solely on this dialog? I would be fine with the app restarting with the same "parent" activity and the dialog above it (that is, just as I left it), or with just the parent activity running and the dialog dismissed.
In this case, you should use a call to finish() in your Dialog code. You want to do this when the user transitions away from your app (which can happen when they go to the home button, they get a call, etc...). In this case, you would want to make a call to finish() in the onStop() of the Dialog. Calls to finish the current activity remove it from the stack, getting you essentially the behavior you describe.
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.
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