On Android, if I added a Dialog into the screen and another Activity opens, how can I get from that Activity all the open Dialogs?
I need to close one of them.
You should create a class and extend DialogFragment and in onCreateDialogreturn your dialog, then show this dialog fragment with a specific tag , after that , you can find this dialog by using activity.getFragmentManager().findFragmentByTag("the tag").
Don't hold a reference to your dialog (local or global) because the activity can get recreated and it will leak the reference in the memory, just get the reference with the findFragment method.
If you are in a fragment , you can use fragment.getChildFragmentManager(...) and do the same.
Good luck!
When another activity opens, your current activity will go through the onPause() state. More info on it can be found here.
In the onPause() call dialog.dismiss() and you should be set.
Related
On click of a certain button in class MainActivity, a dialog is shown of some fragment FragDialog:
FragDialog cf = new FragDialog().newInstace();
cf.show(getSupportFragmentManager(), "dialog");
When a hardware back button is pressed OR when an activity MainActivity is focussed, the dialog is dismissed and I returned to the activity MainActivity.
The methods onResume(), onAttach() of activity MainActivity doesn't get called after dialog dismiss.
The idea is to refresh the activity MainActivity after the dialog dismiss in order to get the changed view according to fields selected in the dialog fragment FragDialog.
As mentioned in the doc DialogFragment,
// DialogFragment.show() will take care of adding the fragment
// in a transaction. We also want to remove any currently showing
// dialog, so make our own transaction and take care of that here.
But adding remove() seems pointless as it doesn't getting called, even dialog is dismissing without it.
I wondered that if this will get called, then I can start the activity MainActivity again to reflect the changes.
while you call dialog you are not loosing contrxt of activity, it is not going in inactive state. All you need just make an event to activity when your dialog is closed, it is quite easy if you cancel it by button. Then you just munnually change your View.
Here is nice article about Dialogs
http://developer.android.com/guide/topics/ui/dialogs.html
I have an quiz app. and i want the user to choose the type of questions from alert dialog . so the alert dialog has a check boxes buttons so if the user can select more then on type. when the user click the positive button in alert dialog it will send info to if condition then the condition will filter the questions and then will pass it to an array. the problem is the array its inside oncreate and i want the alert dialog to be in same class so when i run the class oncreate will start and i get null exception because i didn't pass any info to the array.
How can i delay oncreate() and make it load only after the user click on positive button of alert dialog.
The alert dialog will be in same class .
So how can i do it ? is it possible?
and thanks.
onCreate() is the first called when the activity is created per the android activity lifecycle so, no. I'm not sure why you would want it in the same activity if you don't want it to start unless they click 'OK'. Put it in it's own class or the previous activity.
No you cannot, if you attempt to delay something within the OnCreate() method within an activity it will throw an exception as you don't have a context to place the alert dialog in at that time. As codeMagic previously stated, if you have a prior activity before this one, your best bet would be to create the alert prior to calling startActivity on that intent.
I would suggest to use Fragments in this case. Load Activty, show dialog and based on the answer you will display "correct answer fragment" or "invalid answer fragment".
With this solution activity is loaded (no need to delay) and you just dynamically change the content of the activity.
Try using the AsyncTask class. The onPostExecute method may do the job you're looking for ..
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.
Suppose i am having an activity (let's say activity1) and i have given a command for doing some long process. Mean while i am starting another activity (activity2), during this time if the activity1 finishes the process and shows the result in an alert box, then how can i make this alert box of activity1 appear over activity2? What i have noticed is that, the alert box of activity1 is only visible when i move back to activity1. Is there any way to do this? Ignore if the question is irrelevant, as i am just a beginner in android.
Before using two activity, you have to save the state of the activity and restore the another activity while come front. This is easily done by onRetainNonConfigurationInstance() and getLastNonConfigurationInstance() method. Please refer this link.
I've got an activity, call it A, that when initially opened I would like to programmatically open a another dialog theme activity, B, over the top. The user would select some info from the dialog activity and return to activity A. When resuming to activity A at some later point the data set would be available, so the dialog would not need to be shown.
The problem is attempting to start the dialog activity from activity A's onResume or onCreate methods causes strange and undesirable behaviour. The task appears to kind of freezes for a good few seconds and then the dialog is displayed correctly, but activity A isn't shown behind.
Thanks
Peter
check this:
http://developer.android.com/resources/samples/ApiDemos/src/com/example/android/apis/app/CustomDialogActivity.html
i think u should create activity as a dialog.then it helps
u can set style and theme for ur activity by this..