Issue
I'm using AsyncTask for fill data in textViews, images, other views with soap object. but when I change Orientation to landscape the AsyncTask repeat the process and show Facebook shimmer again. how to stop AsynkTask when fetching my data is done?
Main Activity:
public class MainActivity extends AppCompatActivity {
TextView helloTxt;
RelativeLayout rel;
boolean done = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helloTxt = (TextView)findViewById(R.id.helloTxt);
rel = (RelativeLayout) findViewById(R.id.rel);
// call the inner Class from here
new callSoapObject().execute();
}
This is AsyncTask Inner Class :
private class callSoapObject extends AsyncTask<String,Object,String>{
private final String NameSpace = "https://tempuri.org/";
private final String URL = "https://192.168.0.102/Service.svc/soap";
final String Method_Name = "DoWork";
final String SOAP_ACTION = "https://tempuri.org/IService/DoWork";
public int TimeOut = 5000;
String response;
ShimmerFrameLayout container;
@Override
protected void onPreExecute() {
super.onPreExecute();
//Start Progress bar or placeHolder
container = (ShimmerFrameLayout) findViewById(R.id.shimmer_view_container);
container.startShimmerAnimation();
}
@Override
protected String doInBackground(String... params) {
// create SoapObj
SoapObject request = new SoapObject(NameSpace, Method_Name);
SoapSerializationEnvelope Envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
Envelope.dotNet = true;
Envelope.setOutputSoapObject(request);
HttpTransportSE transportSE = new HttpTransportSE(URL, TimeOut);
try {
transportSE.call(SOAP_ACTION, Envelope);
response = (String) Envelope.getResponse();
} catch (Exception e) {
e.printStackTrace();
Log.e("Error", e.toString());
}
return response;
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
// stop progressBar
container.stopShimmerAnimation();
if (result != null) {
helloTxt.setText(result);
} else {
Toast.makeText(MainActivity.this, "Something went wrong", Toast.LENGTH_LONG).show();
rel.setBackgroundResource(0);
rel.setMinimumWidth(helloTxt.getWidth());
}
}
}
}
Solution
If you're trying to handle orientation change with background task, then I think you can use AsyncTaskLoader instead of AsyncTask. AsyncTaskLoader will handle orientation change for you.
Here you can find differences between AsyncTask and AsyncTaskLoader!
Here you can find AsyncTaskLoader Tutorial
Here is the official documentation of AsyncTaskLoader
Answered By - Parag Pawar
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.