2012-05-21 35 views
5

मैंने इस कथन का परीक्षण किया है कि AsyncTasks को उनकी लॉन्चिंग गतिविधि के साथ नष्ट नहीं किया गया है। और यह सच है।एंड्रॉइड ऑनस्ट्रॉय() के बाद AsyncTask से फिर से कनेक्ट कैसे करें और क्रिएट() पर फिर से लॉन्च करें?

मैंने AsyncTask को केवल 1 मिनट के लिए प्रत्येक 3 सेकंड में Log.i() संदेश प्रकाशित किया। और मैंने गतिविधि के onDestroy() विधि में Log.i() मैसेज डाला।

मुझे लगता है कि गतिविधि नष्ट हो गई है, लेकिन AsyncTask सभी 20 Log.i() संदेशों को समाप्त होने तक चल रहा है।

और मैं उलझन में हूं।

  1. क्या होगा यदि AsyncTask को नष्ट यूआई में publishProgress() था?
    मुझे लगता है कि कुछ प्रकार का अपवाद होगा, है ना?

  2. क्या होगा यदि AsyncTask class Application के वैश्विक चर में डेटा संग्रहीत करता है?
    यहां कोई विचार नहीं, NullPointer अपवाद?

  3. यदि ऐप पुनरारंभ होता है तो क्या होगा?
    यह शायद एक नया AsyncTask लॉन्च करेगा। क्या यह अभी भी AsyncTask चल रहा है?

  4. क्या मां ऐप नष्ट होने के बाद AsyncTask अमर है?
    शायद हां, यूआई एप्लिकेशन अब और दिखाई नहीं दे रहा है, तो सभी लॉगकैट ऐप्स लॉगिंग संदेशों को कैसे रखते हैं, शायद नष्ट हो जाएंगे? और जब आप उन्हें फिर से खोलते हैं तो वे आपको 'मृत' होने पर उत्पन्न हुए संदेश दिखाते हैं।

यह सब एक चर्चा की तरह लगता है, लेकिन सवाल शीर्षक में है। मेरे पास यह अनाथ AsyncTask है, जिसे ऐप को फिर से चालू करने के लिए मैं फिर से कनेक्ट करना चाहता हूं, लेकिन मुझे नहीं पता कि यह कैसे करना है।

मैं यह बताना भूल गया कि यह बहुत महत्वपूर्ण क्यों है। एक अभिविन्यास परिवर्तन होता है जब ऐप नष्ट हो जाता है। और मैं AsyncTask द्वारा उत्पादित डेटा को खोना नहीं चाहता, मैं इसे रोकना और इसे पुनरारंभ नहीं करना चाहता हूं। मैं बस इसे जारी रखना चाहता हूं और अभिविन्यास परिवर्तन किए जाने के बाद पुनः कनेक्ट करना चाहता हूं।

उत्तर

1

मुझे उम्मीद है कि मुझे यह सही मिला है क्योंकि यह अब पुराने कोड से है (अब मैं IntentService का उपयोग करने के लिए उपयोग करता हूं)।

यह

public class MyMainActivity extends Activity { 

    FileDownloader fdl = null; 

    ... 

    // This is an inner class of my main Activity 
    private class FileDownloader extends AsyncTask<String, String, Boolean> { 
     private MyMainActivity parentActivity = null; 

     protected void setParentActivity(MyMainActivity parentActivity) { 
      this.parentActivity = parentActivity; 
     } 

     public FileDownloader(MyMainActivity parentActivity) { 
      this.parentActivity = parentActivity; 
     } 

     // Rest of normal AsyncTask methods here 

    } 
} 

कुंजी 'सहेजें' AsyncTask को onRetainNonConfigurationInstance() उपयोग करने के लिए है ... क्या मैं मूल रूप से किया था जब मैं अपने मुख्य Activity में फ़ाइलों को डाउनलोड है।

Override 
public Object onRetainNonConfigurationInstance() { 

    // If it exists then we MUST set the parent Activity to null 
    // before returning it as once the orientation re-creates the 
    // Activity, the original Context will be invalid 

    if (fdl != null) 
     fdl.setParentActivity(null); 
    return(fdl); 
} 

मैं तो एक विधि doDownload() कहा जाता है जहाँ से onResume() एक अगर Boolean का संकेत downloadComplete सच है कहा जाता है। BooleanonPostExecute(...)FileDownloader की विधि में सेट है।

private void doDownload() { 
    // Retrieve the FileDownloader instance if previousy retained 
    fdl = (FileDownloader)getLastNonConfigurationInstance(); 

    // If it's not null, set the Context to use for progress updates and 
    // to manipulate any UI elements in onPostExecute(...) 
    if (fdl != null) 
     fdl.setParentActivity(this); 
    else { 
     // If we got here fdl is null so an instance hasn't been retained 
     String[] downloadFileList = this.getResources().getStringArray(R.array.full_download_file_list); 
     fdl = new FileDownloader(this); 
     fdl.execute(downloadFileList); 
    } 
}