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()
Related
The following method:KeyChain.choosePrivateKeyAlias creates system dialog which allows user to choose a key alias from a list. As one of the arguments it takes Activity context which will be used to spawn this dialog. When selection is made it will call a callback.
In my scenario user doesn't select any alias but presses "Home" button. I would like to intercept this event. How can I do that?
Also I would like to cancel this dialog programatically. In this case I need some way to access this child Activity (please note that choosePrivateKeyAlias doesn't return any "handle" to the new dialog). Is it possible to access child Activity without any references (handle/id/etc.) to it?
There is no way to programatically end that activity because it is a system activity. It's the same as launching the browser on your device or the contact list. The only way to exit it is to press back and it will close that activity and resume yours.
According to my understanding, if you want to dismiss that dialog. Then as far as I know about Android OS. There are lifecycle methods for Activity. So I think
IF YOU CALL "yourDailog.dismiss()" INSIDE YOUR ACTIVITY's "onStop()". SO YOUR PROBLEM WILL BE SOLVED
If you do it in above way, so whenever you press HOME button, It will call onStop method of your activity. At that time, It will dismiss that dialog.
This is the scenario. I have 2 activities in my application and a Dialog activity that is started when I click on the notification created by my application.
The problem is that when I click on the notification, only the Dialog should show, not the other activity of my app, if it was stopped on pressing the home button.
When I close my application by pressing the back button, the dialog activity shows the dialog, but when the application is running in the background, that activity also opens up on creating the dialog activity.
I use #android:style/Theme.Dialog for my dialog activity.
How to only show the Dialog activity, not other activities in the backgroud?
The solution to the OP issue is setting a different Task affinity for that activity in the Manifest.
<activity android:name="MyIndependentActivity"
android:theme="#android:style/Theme.Dialog"
android:taskAffinity="a_unique_id">
The taskAffinity string must be different from the package name (com.whatever.myapp)
I also use android:excludeFromRecents="true", to hide the dialog from the recent apps list, as it usually makes no sense returning to a dialog.
More info: http://developer.android.com/guide/components/tasks-and-back-stack.html#Affinities
Yeah that'll happen.
When you declare a theme of Dialog it affects the activity lifecycle and the previous activity doesn't go into onStop so some Android functions still think it's the active activity which is technially true as your dialog Activity is acting like a dialog.
One possible work around if you don't 'care' that you can see the previous activity behind the dialog is to change the dialog to be a DialogFragment, put the theme of dialog on the fragment and show this in it's own activity, that'll do it.
The way I have done this (and this can't be the best way to do this) is to have some logic in the activity.onPause() and activity.onResume() methods, that will perform actions based on what I want. My experience is more around separate activities and transitioning between them than using dialogs alot.
You can pass information between activities through setResult(). This will enable you to work out why the child activity has decided to close. That combined with the onResume function should enable you to disable the parent activity.
To override the dialog so that the other activity is not visible behind it, is probably to use the onPause() method to make it go translucent.
I have found onStop() very irratic to use and often unnecessary. The reason for this, is that it is called unpredictably from a developers point of view because onStop can be called based at strange times based on whether the OS has enough memory etc. onPause however in my experience is always called predicably.
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.
I have the following problem:
I have an Activity where a user can start a web search showing a new activity to show a progress bar until the results are shown. Now the user can either wait for the results or maybe think about the search parameters, hit the back button and triggering a new search. The search is running in an Async Task and therefor still running if the user hits back.
At the moment the thread finishes it calls some methods on the old activity causing the activity to show a dialog.
This causes the system to crash because the dialog tries to show itself with a reference to an activity that is not longer present on the screen.
How can I achieve a dialog that is only shown if the activity is still active?
Call isFinishing() on your activity.
i have the following question.
I have an activity which is showing a progress bar while a service is downloading data from an API.
I want that when i press Home and relaunch my program the activity and which is not the first activity called but it is in the stack was recovered in order to continue showing the progress.
I have read about the cycle of life of activities but i don't find a clear solution.
The same thing happens If i am in the activity which is showing the progress bar and i press a button that takes me to another activity, when i go back, can i recover the old activity instead launch one new?
Anyone helps me?
Thanks
The problem is that pressing the home button will erase the whole activity stack. That means there is no possibility to go back to the activity it even is not certain that the activity still exists.
If this a progress that is interesting for the user that it is still running you could display a notification bar icon until the progress is finished. I think you can specify a special intent for clicking on the notification bar and filter this intent with your activity. That way you would go back to the activity. But you still face the problem that the activity is saved and has no reference to the background thread doing the work.
If your Activity has left the stack its finish method is called. You shouldn't try to reuse this activity later on. The best way is to think of a way that the whole state of the activity can be saved and restored later on. To restore a reference to the background thread doing the work you could subclass the application class and save a reference to the running task in your subclass.