我使用AsyncTask在textViews,图像,其他视图中使用soap对象填充数据。 但当我将Orientation改为横向时,AsyncTask重复这个过程并再次显示Facebook闪烁。 在获取数据时如何停止AsynkTask?
主要活动:
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();
}
这是AsyncTask内部类:
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());
}
}
}
}
答案 0 :(得分:0)
在我看来,当您更改方向时,您不希望使用不同的布局和资源,可以通过在AndroidManifest
文件中添加以下配置来避免销毁和重新创建活动,从而防止出现此问题:
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize|keyboardHidden"/>
Here您可以找到处理方向更改的其他解决方案。
答案 1 :(得分:0)