You're right... I have forgotten to save the handler's reference.
Yes, it's strange that it works if i rotate twice the screen... :(

I tried this modify, but it doesn't work yet.

@Override
    public Object onRetainNonConfigurationInstance() {
        Log.d("DEBUG", "onRetainNonConfigurationInstance() saving
listaGP");
        Vector<Object> vect = new Vector<Object>();
        vect.add(listaGP);      //save list
        vect.add(onDownloadFinished);   //save handler
        return vect;
    }

In this way would I have to be able to save the references of both?

On 10 Feb, 20:53, Christoph Renner <[email protected]> wrote:
> Your reference to the Handler and the ArrayList in ConnectTask no
> longer point to the fields of the Activity if it is destroyed and
> recreated. This meens that the the thread cannot update the view
> properly. I have no idea why it works for you when change the screen
> rotation twice.
>
>
>
> On Wed, Feb 10, 2010 at 11:26 AM, Noodles <[email protected]> wrote:
> > Hi Chrigi! Thanks for your answer.
>
> > My thread has a reference to the ListView.
> > But I'm not sure that this is the reason, maybe it could be... but If
> > you are right why, when i rotate display for the second times, do i
> > see the list with data? If i lost the reference to the thread's
> > listview, i would lose also then. or not?
>
> > I show you a part of my code, maybe it can be usefull to understand
> > better my problem.
>
> > public class HelloMotoGp extends ListActivity {
>
> >        /*
> >         *      VARIABILI
> >         */
> >    protected ArrayList<XMLitem> listaGP = new ArrayList<XMLitem>();    //
> > creo la lista che conterrà i GP letti da XML
> >        private ProgressDialog dialog=null;
> >        protected Handler onDownloadFinished;
> >        ConnectTask downloadTk;
> >        Thread thread;
>
> >        /** Called when the activity is first created. */
> >   �...@override
> >    public void onCreate(Bundle savedInstanceState) {
> >        super.onCreate(savedInstanceState);
> >        Log.d("DEBUG", "on Create()");
> >        boolean restoreFlag = restore();
> >        if(!restoreFlag){
> >                manageDialog();
> >                downloadData();
> >        }
>
> >    }
>
> >   �...@override
> >    public void onStop(){
> >        super.onStop();
> >        Log.d("DEBUG", "onStop()");
> >        if(dialog!=null && dialog.isShowing()){
> >                dialog.dismiss();
> >                Log.d("DEBUG", "dialog dismiss onStop()");
> >        }
>
> >    }
>
> >    private void downloadData(){
>
> >        Log.d("DEBUG", "onDownload()");
> >        //creo il thread associato al task Connect
> >            downloadTk=new ConnectTask(url,listaGP, onDownloadFinished);
> >            thread=new Thread(downloadTk);
> >            thread.start();
> >            Log.d("DEBUG", "thread started");
>
> >    }
>
> >    private void populateList(){
> >        setListAdapter(new myAdapter(this, listaGP)); //my custom adapter
> >        Log.d("DEBUG", "populateList()");
> >    }
>
> >   �...@override
> >    protected void onListItemClick(ListView l, View v, int position,
> > long id) {
> >        super.onListItemClick(l, v, position, id);
> >        Intent i = new Intent(this, GaraView.class);
> >        i.putExtra(GaraView.ITEM_LINK,
> > listaGP.get(position).getDescription());
> >        startActivity(i);
> >    }
>
> >   �...@override
> >    public Object onRetainNonConfigurationInstance() {
> >        Log.d("DEBUG", "onRetainNonConfigurationInstance() saving
> > listaGP");
> >        Vector<Object> vect = new Vector<Object>();
> >        vect.add(listaGP);
> >        return vect;
> >    }
>
> >        private boolean restore() {
> >        Vector<Object> obj =
> > (Vector<Object>)getLastNonConfigurationInstance();
> >        if(obj!=null){
> >                listaGP = (ArrayList<XMLitem>) obj.elementAt(0);
> >                //mostro la lista a video se è vuota
> >                if(!listaGP.isEmpty()){
> >                        Log.d("DEBUG", "ListaGP full");
> >                        setContentView(R.layout.main);
> >                        populateList();
> >                        return true;
> >                }else{
> >                        Log.d("DEBUG", "ListaGP, empty");
> >                        //mostro la finestra di download in corso
> >                        return true;
> >                }
> >        }else
> >                Log.d("DEBUG", "obj NULL");
> >                return false;
> >    }
>
> >    private void manageDialog(){
> >        //mostro la finestra di download in corso
> >        dialog = ProgressDialog.show(this,"Downloading...", "Updating
> > Data", true, false);
>
> >        //handler utilizzato per determinare se il download è terminato
> >        onDownloadFinished = new Handler(){
> >               �...@override
> >                public void handleMessage(Message m){
> >                        Log.d("DEBUG", "Handler Captured");
> >                        if(dialog!=null && dialog.isShowing()){
> >                                dialog.dismiss();
> >                                Log.d("DEBUG", "DIALOG was showing now is 
> > DISMISSED");
> >                        }
>
> >                        if(listaGP.isEmpty())
> >                                Log.d("DEBUG", "the list is empty!!!");
> >                        else{
> >                                //mostro la lista a video
> >                                setContentView(R.layout.main);
> >                                populateList();
> >                                Log.d("DEBUG", "Show list after Handler");
> >                        }
> >                }
> >                };
>
> >    }
>
> > }
>
> > ***MY RUNNABLE****
>
> > public class ConnectTask implements Runnable{
>
> >        private String url;     //url sorgente xml
> >        private Handler downloadFinished;
> >        private boolean isListDownload; //flag che indica quale download sto
> > effettuando
> >        ArrayList<XMLitem> lista;       //SD per ListActivity
> >        String [] array;        //SD per la GaraVIew Activity
>
> >        public ConnectTask(String url, ArrayList<XMLitem> list, Handler h){
> >                this.url=url;
> >                this.lista=list;
> >                this.downloadFinished = h;
> >                this.isListDownload = true;
>
> >        }
>
> >        public void run(){
>
> >        /*** my thread work... ***/
>
> >        //send to the main activity a message to signal the thread has
> > terminated. This message will be caputerd with an handler in the
> > activity
> >        Message msg = new Message();
> >        msg.what = HelloMotoGp.DOWNLOAD_COMPLETED;
> >        this.downloadFinished.sendMessage(msg);
> >        Log.d("DEBUG", "thread termina.");
>
> >        }
>
> > }
>
> > On 9 Feb, 11:20, Christoph Renner <[email protected]> wrote:
> >> Hi Noodles,
>
> >> I guess the problem is the following: your thread has a reference to
> >> the Activity or ListView. When the download is finished the thread
> >> uses this reference to add the items to the ListView. However when the
> >> screen orientation has changed, the activity has been destroyed and
> >> recreated and the reference points not anymore to the activity (and
> >> listview) which is displayed. Therefore your adding items (e.g. by
> >> calling setAdapter) on a listview which is not displayed.
>
> >> Possible solution:
> >>  - Thread has reference to activity (thread.activity)
> >>  - when onRetainNonConfigurationInstance is called set thread.activity
> >> = null and return a reference to the thread
> >>  - onCreate: if getLastNonConfigurationInstance returns state (not
> >> null) call state.thread.setActivity(this)
> >>  - whenever your thread wants to access the activity (or the list
> >> view) it needs to make sure that thread.activity != null, if
> >> thread.activity == null the action needs to be cached for later
> >> execution (for example by using a list of runnables)
>
> >> I hope that helps, if something is unclear or you have additional
> >> questions don't hesitate to ask!
>
> >> Chrigi
>
> >> On Mon, Feb 8, 2010 at 5:39 PM, Noodles <[email protected]> wrote:
> >> > I have a problem with rotation, during my ListActivity is loading data
> >> > from the network.
> >> > To do it I use a background thread which recovers data and put them
> >> > inside an ArrayList. While this thread works, there is Progress dialog
> >> > activated in foreground.
>
> >> > For the rotation I use the method with
> >> > onRetainNonConfigurationInstance() to save my ArrayList's state. After
> >> > android calls this method, I call dismiss() on the progress dialog on
> >> > onStop(). Then the activity is created as new with onCreate(), but it
> >> > use the getLastNonConfigurationInstance() to recover the ArrayList's
> >> > state.
>
> >> > Now, my problem...
>
> >> > If I rotate from portrait to lanscape when the download is terminated
> >> > and I see the data in portrait yet, everything is ok.
> >> > If I rotate during progress dialog is showing I have two cases:
>
> >> > 1-  onRetainNonConfigurationInstance() saves the arraylist fully
> >> > recovered, the thread terminates and everything is ok.
> >> > 2-  onRetainNonConfigurationInstance() saves the arraylist empty, i
> >> > think the download isn't finished yet.
> >> > The onCreate is called and my list is empty, the thread terminates,
> >> > but i don't see anything in my list.
>
> >> > ... if I am in this case and I still rotate from landscape to portrait
> >> > now I can see my list full with all the data.
>
> >> > Why?? Where am I wrong?
>
> >> > p.s. sorry for my english, I hope everything is clear.
>
> >> > --
> >> > You received this message because you are subscribed to the Google
> >> > Groups "Android Developers" group.
> >> > To post to this group, send email to [email protected]
> >> > To unsubscribe from this group, send email to
> >> > [email protected]
> >> > For more options, visit this group at
> >> >http://groups.google.com/group/android-developers?hl=en
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to...
>
> leggi tutto

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

Reply via email to