0

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());
        }
    }
}
}
akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
  • Possible duplicate of [Android: Cancel Async Task](https://stackoverflow.com/questions/6039158/android-cancel-async-task) – udit7395 Mar 31 '18 at 10:19

2 Answers2

0

As it seems to me that you don't want to use a different layout and resources when orientation changes, you can prevent this problem by avoiding destroy and recreate of your activity by adding below configuration to your AndroidManifest file:

<activity
    android:name=".MainActivity"
    android:configChanges="orientation|screenSize|keyboardHidden"/>

Here you can find other solution for handling orientation change.

Akram
  • 2,158
  • 1
  • 17
  • 24
0

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

Parag Pawar
  • 827
  • 3
  • 12
  • 23