How to show a yes/no dialog in AsyncTask onPostExecute() - android

I need to show a yes/no dialog in AsyncTask.onPostExecute() but I keep getting
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
when the screen is rotated just before the dialog is shown. I had a similar problem showing dialogs in the onActivityResult but have since moved the dialogs to the
onPostResume() as suggested by other posts but I am unable to solve this one.
Is it possible to show a yes/no dialog in the onpostexecute() without causing exception and without using "commitAllowingStateLoss"?
Your help is much appreciated.

I think you should maintain state of your activity in which you are calling the dialog, because every time when activities orientation changes onCreate() function calls and if you are initiating any AsyncTask in it, then it will execute it again. May be that's why you are facing the problem. Just maintain its state and then check it, hope it will help you.
Just add this code in your manifest file for that activity. Just type landscape or portrait according to your need.
android:screenOrientation="portrait/landscape"
Thank you.

Related

Activity has leaked window?

I am having one java class in that class as soon some one purchases our application then it will start downloading and progress dialog has to appear instead it goes to some other page and when i come out of the application and when i restart then it starts downloading.
Please Help me out from this mess...
Thank you
Check the condition for dialog, before showing.
Like this
if(pDialog!=null)
{
if(!pDialog.isShowing())
{
pDialog.show();
}
}
and also while removing the dialog in onPostexecute() check for null.
if Still not works just remove the pDialog and try once with your code.
Two causes for your error happen:
The error will happen if you're trying to show a Dialog after you've exited an Activity.
Also, if an unhandled Exception was thrown in your AsyncTask, which would cause the Activity to shutdown, then an open progress dialog will cause the Exception.
According to the Log you've posted, the error happens after you call pDialog.show() which might be the 1st cause I've mentioned before.
Also you are calling finish() in many parts of your code, maybe one of these calls are making your Activity to stop and leaking your Dialog.
You must check which one of them is finishing your Activity before you show the Dialog. A good solution is to dismiss the Dialog (if it's showing) before calling finish().

Display loading screen using AsyncTask with ListActivity

I've got an app that uses ListActivity to give users a list of actions. When they click one I use an Intent to launch a separate activity.
My problem is that the actions that the app performs take about 20 seconds to finish, and since I don't want the user to receive that nasty ANR dialog, I tried to use AsyncTask to present them with a loading screen in the mean time. I tried using setContentView(R.layout.loading); on onPreExecute(), but it throws a NullPointerException which as far as I have figured out is due to the fact that loading.xml is not "a ListView whose ID is android.R.id.list".
So what can I do now? How can I show that loading screen? Is there a way around this pretty annoying situation? Any help would be greatly appreciated. Thanks!
I am not sure exactly what your use case is; you have a list of items that are populated immediately, and upon selecting one an action is taken? The action that is taken is to launch another Activity which performs background processing?
Or does it take that long to populate the list of actions?
If the former, you can use an AsyncTask for the long-running activity instead of an Intent to launch another Activity: in the callback you get for the click on the item in question, you would create the AsyncTask, and in doInBackground you would perform the long-running activity, with onPostExecute refreshing or manipulating your list as necessary.
Another thing to consider is using a dialog box to show a loading screen, if the loading is required to happen before you launch a new Activity.
If you can further describe your use case, I can help you more.
It's not the loading screen you need to have on the AsyncTask, it's that 20-second Activity initialization. I would look for a way to do all the setup in a background thread in a Service while the user is free to merrily bop around in other Activities. I'd try hard to find a way not to just stall the user for 20 seconds. Maybe take them to the target Activity and show them data cached from their last visit until the new set is ready.
Fire up and display your loading dialogs in your onCreate() of the Activity being called, then call Dialog.dismiss() in your AsyncTask's onPostExecute().

Android dialog rotation issue.

I have a Dialog which depends on information from the current view. This works fine until the unit is rotated at which point the dialog tries to display before the view is built. The dialog is non essential and I'm happy to scrap it on rotation rather than try to replicate the information and hold it for the dialog to be reconstructed.
I've tried calling removeDialog(PHOTO_CREDIT_DIALOG) from the activities onPause method but it doesn't seem to do anything. I've also held a reference to the dialog and tried calling dismiss() in onPause. Also to no avail. The same call when run from a button on the dialog does infact remove it.
thanks,
m
Does onPause get called ? What if you try to remove the Dialog in onConfigurationChanged instead?
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange

Android: The AlertDialog is invisible when the Activity back to foreground

This question is related to The AlertDialog is invisible when the Activity back to foreground post.
I have the same problem. The previous post is old, and have no answer. Any suggestions how to solve that problem ? Thanks...
For some reason, Dialogs' states must be handled by the developer.
Simply keep a reference to the dialog showing
For example
Dialog showingDialog=null;
Now in onResume()
if(showingdialog!=null)
//show the dialog and maybe resume some state
Have you tried to re-display the AlertDialog when in the activities onResume(). Using Google developers example you would be able to create an instance of this dialog and just recall it.
http://developer.android.com/guide/topics/ui/dialogs.html#AlertDialog
Hope that helps.
Also we all create Dialogs on the fly whenever we need them, we should not.
Android way is (by the book) to override an onCreateDialog(int) and showDialog(int) in our activities so that our dialogs can be managed by the activity lifecycle.
Another way to do so is to use myDialog.setOwnerActivity(MyActivity.this) to tell the dialog it is managed by the activity.

Loading Dialog while rotating device

I have an activity with in which there is a async task that will do some download stuff. AT the time of downlaoding it will show a loading dialog.
My problem is, it worked fine for me when me doing it in only one orentiaon. But when i rotate at the time of download, it shows window leaked and will crash at the
dialog.cancel in my post excute.
From my study on it more i understood it due the change in the context when device is rotated.
That is when a device is rotated the activity will be recreated so the context will be changed.
But i have created the dialog with old one and that wasn't the current context. So when i cancel it it shows error
What is the solution for this, any idea frnds.
Me using honeycomb, me tried but with fragment but didnt get a good sample for that. Me now mainly trying that,
if anyone can give me some links for that it will be
great
First of all: open your dialog using the showDialog method (there are a lot of examples in the official documentation). If you do so, the activity will take care of dismissing the dialog on destroy, and re-showing it after the activity has been recreated.
Also... if the dialog shows a progress bar (not a wheel), you will want to update the progress of the dialog after orientation changes. In order to do so, I recommend to use the onRetainNonConfigurationInstance to return the current state of the dialog and/or the activity itself. Then, you can use getLastNonConfigurationInstance to recover that state. Google about those two methods if you want to see examples.
Another thing to keep in mind: if you are updating the state of the dialog an/or any other UI element from the AsyncTask, you must be aware that after the activity is recreated, the AsyncTask may be pointing to the wrong UI references. In order to handle this, you can create a proxy class (Proxy design pattern) to detach the AsyncTask progress notifications from the current UI elements.

Categories

Resources