AlertDialog.dismiss()? - android

I am new in android and I am learning from developer.android.com site. Then I came across to AlertDialog.dismiss() where in site it is written that
This method Dismiss dialog and remove it from the screen. This method can be
invoked safely from any thread. Note that you should not override this
method to do cleanup when the dialog is dismissed, instead implement
that in onStop().
But I did not understand the mean of this line-
Note that you should not override this method to do cleanup when the
dialog is dismissed, instead implement that in onStop()
what is the mean of above line?
`.

AlertDialog.dismiss() uses to dismiss the dialog if it's opened up as describe at developer site
Note that you should not override this method to do cleanup when the dialog is dismissed, instead implement that in onStop().
The above statement simply means that as we used to garbage collect object which is no more referenced in class and avail for garbage collect. They are simpling stating that the approach like avail for garbage collection also applies in here but there are eligible inside onStop() of Activity.
So better to use it as onStop() as it's the last call of Activity Life Cycle which can dismissed your alertdialog. If it incase is there on the screen without dismissal.

Related

onDestroy method is not calling in android o version

I want to call method in ondestroy override method but that override method is not calling android o only. Why it is not calling is there any alternative for that please any one help me to resolve my issue.Thanks in advance.
Given your comment that you are overriding onDestroy() in the activity and killing your app manually:
Please be aware that there it's not guaranteed that the onDestroy() will be called properly. Because of that, you should not perform critical actions there.
See an excerpt from the Documentation:
Note: do not count on this method being called as a place for saving data! For example, if an activity is editing data in a content provider, those edits should be committed in either onPause() or onSaveInstanceState(Bundle), not here. This method is usually implemented to free resources like threads that are associated with an activity, so that a destroyed activity does not leave such things around while the rest of its application is still running. There are situations where the system will simply kill the activity's hosting process without calling this method (or any others) in it, so it should not be used to do things that are intended to remain around after the process goes away.
Need to work in android o, Could you send your codes?
public void onDestroy() {
...
// Must always call the super method at the end.
super.onDestroy();
}

How to judge after onSaveInstanceState in Android Development?

sometimes, i will get a
java.lang.IllegalStateException Can not perform this action after
onSaveInstanceState
when i try to show a DialogFragment.
so now the question is how i can write a if statement to judge current state before i involve the show method like this?
if(!isAfterOnSaveInstanceState())
dialog.show(getSupportFragment(),"MY DIALOG");
because i notice that the DialogFragment own the method dismissAllowingStateLoss() but lack of a method to allow state loss when show, maybe the design of the DialogFragent want developers to judge before call show method.
Thanks in advance.
onSaveInstanceState(): If called, this method will occur before onStop(). There are no guarantees about whether it will occur before or after onPause().
you can check isFinishing() before show your DialogFragment
if(!isFinishing()) {
// your code goes here...
}
more detail:link-1,link-2,link-3

Force method to run *after* AlertDialog

I'm calling an AlertDialog that exists in a different class. Directly after, I use a callback method to refresh the view. However, it appears the dialog runs on a separate thread, because the callback method gets called before the user makes a choice in the AlertDialog, instead of after.
How do I make sure the callback method is only called after the user has made a choice?
mClientManager.deleteClientConfirmationDialog ( getActivity (), id );
mCallback.refresh ();
However, it appears the dialog runs on a separate thread
No, it does not.
because the callback method gets called before the user makes a choice in the AlertDialog, instead of after
The dialog is not being shown until well after deleteClientConfirmationDialog() and refresh() have been called. You may be requesting the dialog to be shown somewhere in there, but all that does it put a message on a queue, to be processed by the main application thread, sometime after you return control of that thread to the framework.
How do I make sure the callback method is only called after the user has made a choice?
Put the logic in an appropriate event listener. Lance's suggestion of setOnDismissListener() is a reasonable choice, if you want refresh() to be called regardless of how or why the dialog is going away.

Activity can do without onStart() particularly and onStop() as well?

This is a text I have copied and pasted from this training tutorial.
"Because the system retains your Activity instance in system memory when it is stopped, it's possible that you don't need to implement the onStop() and onRestart() (or even onStart() methods at all. For most activities that are relatively simple, the activity will stop and restart just fine and you might only need to use onPause() to pause ongoing actions and disconnect from system resources."
I don't understand it. Because to the best of my knowledge, an activity is only stopped by calling onStop() and is only started by calling onStart(). How can an activity start at all without an onStart method.
Do you people understand what they mean in this paragraph?
I think they are confusing you with the word "stop" which appears to have multiple meanings in the paragraph.
I would rephrase it as
Because the system retains your Activity instance in system memory
when it is not in the foreground, it's possible that you don't need
to implement the onStop() and onRestart() (or even onStart() methods
at all. For most activities that are relatively simple, the activity
will suspend and restart just fine and you might only need to use
onPause() to pause ongoing actions and disconnect from system
resources.
The point being is that the App can appear to be stopped, when in actual fact, the system has simply paused it and hidden it from the screen. When the user launches it again, the App doesn't need to start (because it technically hasn't stopped), so it is simply resumed.
When you make an Activity and extend the base class Activity, there is already code in the onStop(), onStart(), and onRestart() methods in the base class.
Your activity simply extends these methods, meaning that you could add more code to them by Overriding them.
So, even though Activities are only started and stopped through those methods, you do not have to explicitly override them in your application. In most cases you won't even have to worry about them: They will be called by the base class from which you are extending.
Please make sure, An Activity starts from onCreate method , then onStart is called by system. If you override onStart method then your overridden method will be also called after onCreate method. If you don't override , then default version of onStart is called.
onStop is called after onPause.
Please check this link , and take a look at Activity life cycle . Your concept will be clear.
Difference between onCreate() and onStart()?
you can use an Activity just fine without - if you need to do something special in onPause() you can override the method:
#Override
public void onPause(){
super.onPause();
// Your magic here!
}
Same goes for onStart(), onStop() etc. You don't need to override the methods but you can if you need to do something specific.

How to cancel or dismiss a custom dialog in its onCreate method?

I have created a custom dialog called MyCustomDialog which extends Dialog. I create and show my custom dialog as follows:
new MyCustomDialog(myContext).show();
I override the Dialog.onCreate(Bundle savedInstanceState) method to do my initialisation. I also check in this method whether a certain condition holds and, if not, I would like to dismiss/cancel my dialog. I have tried calling the cancel() and dismiss() methods in my dialog's onCreate(Bundle savedInstanceState) and onStart() methods but it has no effect.
Anyone know how to cancel or dismiss a dialog (from within the dialog) before it shows?
You should place the logic to determine if the dialog is to be shown outside of the onCreate() method. it does not belong there.
Alternatively, rename your show() method showIfRequired() (or something), and add the conditional show logic there.
I know this doesn't technically answer your question, but what you are trying to do is not the correct design. That's a good thing, as doing in the right way is actually simpler.
Also, as a side note, you should using DialogFragment in favor of Dialog. it's available in the v4 support library.
This is for API levels 10 and below:
First you should override onCreateDialog(int id, Bundle args) in the Activity class, is that what you're doing? Dialogs are always created and displayed as part of the Activity. Second, I don't think you can cancel/dismiss a dialog in onCreateDialog because it hasn't actually been created when onCreateDialog is called. That is, you can't cancel/dismiss something that hasn't been created. What you can try is to override onPrepareDialog() instead and do your check to cancel/dismiss the dialog there. At that point the dialog should actually have been created (just not displayed), so you would be able to prevent it from getting displayed if you call cancel/dismiss there.
onPrepareDialog() is the proper place to do any sort of checks and decision making on the dialog that is about to be displayed. This is for APIs prior to Honeycomb.
This is for APIs 11 and later:
If you are using a later API, you should extend DialogFragment instead. In this case I think you can handle the decision making in onCreateView() method of DialogFragment which is similar to onPrepareDialog().
I hope you've read through this:
http://developer.android.com/guide/topics/ui/dialogs.html
or this, depending on your API:
http://developer.android.com/reference/android/app/DialogFragment.html
Overall, perhaps a cleaner solution is to disable the button or mechanism that causes the dialog to show up in the first place? That is, write you code such that Dialog.show() is called only when it really needs to be called. I'd have to know more details about what exactly you're trying to do. For example, say you call Dialog.show() from the onClickListener of a button. you don't really want the user to press a button, expect a dialog, but have it not show up due to some reason the user doesn't understand. A better solution would be to disable the button all together so that it's obvious to the user that this function isn't available due to something else in the application.

Categories

Resources