All my dialogs are their own classes.  They don't extend from
anything.  The beaury of this pattern that DialogClass can contain
many different real Dialog objects and they are usuable from Acitvity
to Activity:

The DialogClass is the container for the real Dialog objects.  It  has
a "dialog" member and 4 methods:

public class DialogClass
{
       Dialog                          dialog;

    public void saveState(Bundle state)
    {
        // Ensure a unique key
        String baseKey = this.getClass().getName();

        if (this.dialog != null)
            state.putBundle(baseKey + KEY_DIALOG_STATE,
this.dialog.onSaveInstanceState());
    }


    public void restoreState(Bundle state, ...)
    {
        String baseKey = getClass().getName();

        if (state.containsKey(baseKey + KEY_DIALOG_STATE))
        {
            showDialog(...);
            this.dialog.onRestoreInstanceState(state.getBundle(baseKey
+ KEY_DIALOG_STATE));
        }
    }


    public void show(..)
    {
        // The NULL check ensure that if the user very quickly taps
some button to bring up the dialog, we only show one
        if (this.dialog == null)
            showDialog(...);
    }


    public void dismissIfShowing()
    {
        if (this.dialog != null)
            this.dialog.dismiss();
    }
}



Then in my Activity I have:

public class MyActivity extends Activity
{
    private DialogClasss diialog  = new DialogClass();

    protected void onCreate(Bundle state)
    {
        super.onCreate(state);

        // Show dialog if it was showing and restore its state if we
are coming back from an orientation change
            if (state != null)
                 this.dialog.restoreState(state);
    }

    protected void onDestroy()
    {
        super.onDestroy();

        this.dialog.dismissIfShowing();
    }


    protected void onSaveInstanceState(Bundle state)
    {
        super.onSaveInstanceState(state);

        this.dialog.saveState(state);
    }


    // Call this method to show your dialog, for example, from an
onClick listener
    private void showDialog()
    {
        this.dialog.show();
    }
}








On Jun 20, 4:11 pm, sahana uday <sahanau...@gmail.com> wrote:
> Ya sure I would like to take a look at it. Thanks alot.
>
> On Jun 20, 11:10 am, Zsolt Vasvari <zvasv...@gmail.com> wrote:
>
>
>
> > I've taken care of thedialog'son rotationissuewith a custom
> > pattern.  I posted it here a few months ago, but was shut down by
> > people.  It's not that simple, but it works very well and the dialogs
> > are preserved perfectly.  If you care, I can post my solution again.
>
> > On Jun 20, 1:08 pm, William Ferguson <william.ferguson...@gmail.com>
> > wrote:
>
> > > Android doesn't handle rotation and dialogs particularly well, mainly
> > > due to rotation causing the Activity to be destroyed and a new one
> > > created. This leaves Dialogs with an invalid Context. Using managed
> > > dialogs (like you are) just shifts the problem, otherwise you get
> > > java.lang.IllegalArgumentException: View not attached to window
> > > manager instead.
>
> > > In Froyo and below calling dismissDialog for an Activity that has
> > > already been destroyed causes the Exception you noted. As far as I can
> > > recall, Gingerbread logs a message but swallows the Exception. I have
> > > tended to add a try/catch around dismissDialog to handle the pre
> > > Gingerbread cases.
>
> > > You can configure your Activity to stop rotation occurring, or your
> > > can code your Activity to handle rotation.
> > > But as Streets of Boston says, you don't want to be firing off your
> > > AsyncTask in onCreate, at least not without checking to see whether
> > > this Activity has already been started/destroyed due to rotation. See
> > > #getLastNonConfigurationInstance.
>
> > > On Jun 20, 1:52 pm, sahana uday <sahanau...@gmail.com> wrote:
>
> > > > Hi,
> > > > I understood what you were trying to say. Here's what I;'m doing now:
>
> > > > class ConnectionsListAddScreen extends Activity{
> > > >        public void onCreate(Bundle savedInstState){
> > > >             super.onCreate(savedInstState);
> > > >             //here on particular button click
> > > >             new ConnReqSenderAsyncTask().execute();
>
> > > >        }
>
> > > >        protectedDialogonCreateDialog(int id) {
> > > >                Dialogdialog= newDialog(ConnectionsListAddScreen.this);
> > > >                 switch(id){
> > > >                         case Constants.LOGIN_DIALOG:    ProgressDialog 
> > > > pg = new
> > > > ProgressDialog(ConnectionsListAddScreen.this);
> > > >                                                         
> > > > pg.setMessage(//some string);
> > > >                                                         
> > > > pg.setCancelable(true);
> > > >                                                        dialog= pg;
> > > >                                                         break;
> > > >                         default:        return super.onCreateDialog(id);
>
> > > >                 }
> > > >                 returndialog;
> > > >         }
>
> > > > }
>
> > > > private class ConnReqSenderAsyncTask extends
> > > > AsyncTask<Void,Void,Void>{
>
> > > >         protected Void doInBackground(Void... params) {
> > > >               //send request to server
> > > >               return null;
> > > >         }
>
> > > >         protected void onPreExecute() {
> > > >                 super.onPreExecute();
> > > >                 showDialog(Constants.LOGIN_DIALOG);
> > > >         }
>
> > > >         protected void onPostExecute(Void result) {
> > > >                 super.onPostExecute(result);
> > > >                 dismissDialog(Constants.LOGIN_DIALOG);
> > > >         }
>
> > > > }
>
> > > > If the above is right. Then the problem is onOrientationChangeit
> > > > throws below exception:
>
> > > > FATAL EXCEPTION: main
> > > > java.lang.IllegalArgumentException: nodialogwith id 5 was ever shown
> > > > via Activity#showDialog
> > > > at android.app.Activity.missingDialog(Activity.java:2590)
> > > > at android.app.Activity.dismissDialog(Activity.java:2575)
> > > > at com.sap.explorer.view.ConnectionsListAddScreen
> > > > $ConnReqSenderAsyncTask.onPostExecute(ConnectionsListAddScreen.java:
> > > > 699)
> > > > at com.sap.explorer.view.ConnectionsListAddScreen
> > > > $ConnReqSenderAsyncTask.onPostExecute(ConnectionsListAddScreen.java:1)
> > > > at android.os.AsyncTask.finish(AsyncTask.java:417)
> > > > at android.os.AsyncTask.access$300(AsyncTask.java:127)
> > > > at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:
> > > > 429)
> > > > at android.os.Handler.dispatchMessage(Handler.java:99)
> > > > at android.os.Looper.loop(Looper.java:123)
> > > > at android.app.ActivityThread.main(ActivityThread.java:4627)
> > > > at java.lang.reflect.Method.invokeNative(Native Method)
> > > > at java.lang.reflect.Method.invoke(Method.java:521)
> > > > at com.android.internal.os.ZygoteInit
> > > > $MethodAndArgsCaller.run(ZygoteInit.java:868)
> > > > at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
>
> > > > Since we are using showDialog(...) and onCreateDialog(...) for
> > > > ProgressDialog display, then shouldn't Android keep a reference of
> > > > same onorientationchange. Can you let me know if there are any
> > > > mistakes in my code or solution to my problem asap.
>
> > > > On Jun 16, 4:02 pm, sahana uday <sahanau...@gmail.com> wrote:
>
> > > > > Hi,
> > > > > Yes I'm sending the request to server in onCreateDialog(), but its an
> > > > > AsyncTask.
> > > > > In onPreExecute() show theprogressdialogand onPostExecute() cancel
> > > > > theprogressdialog.
>
> > > > > Off late I have anotherissuein computing textview height, I have
> > > > > posted thisissueon a separate thread also.
> > > > > The issues is:
> > > > >         How to compute TextView Height before its laid out on screen?
>
> > > > >         I'm not aware how we can use getPaint().ascent() or descent()
> > > > > method
> > > > >         for computing height?
>
> > > > >         Can you please provide an example
>
> > > > > On Jun 6, 6:38 pm, Streets Of Boston <flyingdutc...@gmail.com> wrote:
>
> > > > > > Are you sending a request to the server in the implementation of the
> > > > > > 'onCreateDialog(...)' method? If so, this is not good. The 
> > > > > > onCreateDialog is
> > > > > > called by the OS when the hosting activity is recreated due to a 
> > > > > > config
> > > > > >changewhen adialogis shown using the showDialog method (i.e. an
> > > > > > activity-manageddialog). This is perfectly normal behavior.
>
> > > > > > Call 'showDialog' just *after *you send an (asynchronous) request 
> > > > > > to the
> > > > > > server and call 'dismissDialog' when the server returns a response. 
> > > > > > Usually,
> > > > > > do not code anything in the onCreateDialog except for building an
> > > > > > AlertDialog object.- Hide quoted text -
>
> > > - Show quoted text -- Hide quoted text -
>
> - Show quoted text -

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to