मैं एक हैंडलर कॉलबैक लागू करने का सुझाव दूंगा। आप टुकड़े (या गतिविधि के) हैंडलर को AsyncTask में पास करेंगे, जो AsyncTask समाप्त होने पर कॉल करेगा। AsyncTask भी एक मनमानी वस्तु वापस पास कर सकते हैं।
यहाँ एक उदाहरण AsyncTask, जो मैं अपने आप ही फाइल में है (नहीं subclassed):
public class MyTask extends AsyncTask<Void, String, String> {
private static final String TAG = "MyTask";
private Handler mCallersHandler;
private Candy mObject1;
private Popsicle mObject2;
// Return codes
public static final int MSG_FINISHED = 1001;
public SaveVideoTask(Handler handler, Candy candyCane, Popsicle grapePop) {
this.mCallersHandler = handler;
this.mObject1 = candyCane;
this.mObject2 = grapePop;
}
@Override
protected String doInBackground(Void... params) {
// Do all of the processing that you want to do...
// You already have the private fields because of the constructor
// so you can use mObject1 and mObject2
Dessert objectToReturn = mObject1 + mObject2;
// Tell the handler (usually from the calling thread) that we are finished,
// returning an object with the message
mCallersHandler.sendMessage(Message.obtain(mCallersHandler, MSG_FINISHED, objectToReturn));
return (null);
}
}
इस उदाहरण में कल्पना है कि आपके AsyncTask कैंडी और एक Popsicle का एक टुकड़ा की जरूरत है।फिर यह आपके टुकड़े में मिठाई वापस कर देगा।
आप का निर्माण और के साथ अपने टुकड़ा से एक पंक्ति में AsyncTask चला सकते हैं:
(new MyTask(mFragmentHandler, candyCane, grapePop)).execute();
लेकिन निश्चित रूप से, आप पहली बार टुकड़ा के हैंडलर (myFragmentHandler) स्थापित करने के लिए की आवश्यकता होगी। ऐसा करने के लिए, अपने टुकड़ा (या गतिविधि) की तरह दिखना चाहिए (नोट "Handler.Callback लागू करता है"):
public class MyFragment extends Fragment implements Handler.Callback {
private Handler mFragmentHandler;
private Candy candyCane;
private Popsicle grapePop;
@Override
public void onCreate(Bundle savedInstanceState) {
// Standard creation code
super.onCreate(savedInstanceState);
setRetainInstance(true);
// Create a handler for this fragment
mFragmentHandler = new Handler(this);
// Other stuff...
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
// Inflate the layout
View v = inflater.inflate(R.layout.my_fragment_layout, parent, false);
// The candyCane and grapePop don't need to be set up here, but
// they MUST be set up before the button is pressed.
// Here would be a good place to at least initialize them...
// Perhaps you have a button in "my_fragment_layout" that triggers the AsyncTask...
Button mButton = (Button) v.findViewById(R.id.mButton);
mButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
(new MyTask(mFragmentHandler, candyCane, grapePop)).execute();
}
});
return v;
}
@SuppressWarnings("unchecked")
@Override
public boolean handleMessage(Message msg) {
switch (msg.what) {
case MyTask.MSG_FINISHED:
// Let's see what we are having for dessert
Dessert myDessert = (Dessert) msg.obj;
break;
}
return false;
}
}
आप कोड के इन टुकड़ों का उपयोग करते हैं, एक बटन दबाने की AsyncTask ट्रिगर किया जाएगा। AsyncTask संसाधित होने पर कॉलिंग खंड निष्पादित करना जारी रहेगा। फिर, जब AsyncTask समाप्त हो जाता है, तो यह खंड को एक संदेश भेजेगा कि यह समाप्त हो गया है, और संदेश के साथ ऑब्जेक्ट पास करें। इस बिंदु पर, टुकड़ा संदेश देखेंगे, और जो भी आप चाहते हैं।
नोट: टाइपो हो सकता है। यह एक बहुत बड़े और जटिल कोड से काटा जाता है।
विधि निष्पादन 'AsyncTask' परिणाम न लौटाता है। यदि आप asyncTask से कुछ परिणाम वापस करना चाहते हैं या कुछ डेटा प्रदान करना चाहते हैं, तो [प्रोटोकॉल] (http://stackoverflow.com/a/26820666/2835520) – IgniteCoders