2012/03/26

[Android]Display view first, flash content later

Let view comes out first, then update the content later.
Do not put everything into onCreate(), try to use AsyncTask class to access content in background.

1. Implement a class to inherent AsynTask class
class RunningAsyncTask extends AsyncTask{
   protected void onPreExecute( ) { 
     // system will call this first
     // do whatever view content modification
     // just after thread started.
      bEndThread = false;
   }
   protected void onPostExecute( String value ) { 
     // system will call this when thread finished.
     // do whatever view content modification
     // before thread finished.
      SetAddressText ( value );
      bEndThread = true;
   }
   protected void onProgressUpdate(Integer... values) {  
     // system will call this when publishProgress( )
     // is called in doInBackground( )
     // do whatever view content modification 
     // when the background process.
   } 
   protected String doInBackground(Void... params) {  
     // CAN NOT DO any modification of view content
     // just do any content fetch for longer process time
     // Call publishProgress((int) ((i / (float) count) * 100))
     // giving a chance to update the view during the process time
      String addStr = getCurrentGPSAddress( );
      return addStr;
   }
}
2. In onResume or onCreate to init the thread (never reentry)
  protected void onResume(){
    super.onResume( );
    if( bEndThread == true);
      new RunningAsyncTask().execute();
  }
3. Never Never execute a new AsyncTask when the last one is not finished.

沒有留言: