My app will auto load data when i run app.My problem is when my screen vertical app will load data from server, but when screen rotation horizontal, it load data again. Doing so when rotation screen app will load again data.Can you help me.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nhabep);
new LoadData().execute();
}
// Class load data
private class LoadData extends AsyncTask<Void, Void, Void> {
private ProgressDialog progressDialog;
private JSONArray jArray;
private String result = null;
private InputStream is = null;
private StringBuilder sb = null;
@Override
protected void onPreExecute() {
this.progressDialog = ProgressDialog.show(Nhabep.this, "",
" Loading...");
}
@Override
protected void onPostExecute(final Void unused) {
this.progressDialog.dismiss();
try {
if (flag == false)
{
Toast.makeText(Nhabep.this, "Không có bàn nào được chọn!!", Toast.LENGTH_SHORT).show();
}
else
{
//Hiển thị thông tin các món ăn lên listview
listview = (ListView) findViewById(R.id.listView1);
this.progressDialog.dismiss();
listview.setAdapter(new DataAdapter(Nhabep.this, soban
.toArray(new String[soban.size()]), thoigian
.toArray(new String[thoigian.size()])));
listview.setOnItemClickListener(new OnItemClickListener() {
//xử lý khi chọn các item trên listview
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
String soban2 = soban.get(position);
String cafesua2 = cafesua.get(position);
String cafeda2 = cafeda.get(position);
String cafeden2 = cafeden.get(position);
String duatuoi2 = duatuoi.get(position);
String nuocngot2 = nuocngot.get(position);
String cavienchien2 = cavienchien.get(position);
String goiga2 = goiga.get(position);
String bokho2 = bokho.get(position);
String bunbo2 = bunbo.get(position);
Intent i = new Intent(Nhabep.this, Show.class);
i.putExtra("soban", soban2);
i.putExtra("cafesua", cafesua2);
i.putExtra("cafeda", cafeda2);
i.putExtra("cafeden", cafeden2);
i.putExtra("duatuoi", duatuoi2);
i.putExtra("nuocngot", nuocngot2);
i.putExtra("cavienchien", cavienchien2);
i.putExtra("goiga", goiga2);
i.putExtra("bokho", bokho2);
i.putExtra("bunbo", bunbo2);
startActivity(i);
}
});
Toast.makeText(Nhabep.this, "Thông tin bàn được tải thành công!!", Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
}
}
//kết nối đến sererver và nhân thông tin trả về từ server
@Override
protected Void doInBackground(Void... params) {
try {
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://longvansolution.tk/loadthongtin.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is, "iso-8859-1"), 80);
sb = new StringBuilder();
sb.append(reader.readLine() + "\n");
String line = "0";
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
//Kiểm tra thông tin nhận được từ server
//nếu null sẽ clear các textview
//nếu có thông tin thì đọc thông tin theo Json
if (result.toString().equalsIgnoreCase("null\n"))
{
flag = false;
soban.clear();
thoigian.clear();
cafesua.clear();
cafeda.clear();
cafeden.clear();
duatuoi.clear();
nuocngot.clear();
cavienchien.clear();
goiga.clear();
bokho.clear();
bunbo.clear();
} else
{
//Lấy thông tin theo Json
jArray = new JSONArray(result);
if (jArray != null && jArray.length() > 0)
{
JSONObject json_data = null;
soban.clear();
thoigian.clear();
cafesua.clear();
cafeda.clear();
cafeden.clear();
duatuoi.clear();
nuocngot.clear();
cavienchien.clear();
goiga.clear();
bokho.clear();
bunbo.clear();
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
thoigian1 = json_data.getString("date");
soban1 = json_data.getString("ban");
cafesua1 = json_data.getString("cafesua");
cafeda1 = json_data.getString("cafeda");
cafeden1 = json_data.getString("cafeden");
duatuoi1 = json_data.getString("duatuoi");
nuocngot1 = json_data.getString("nuocngot");
cavienchien1 = json_data.getString("cavienchien");
goiga1 = json_data.getString("goiga");
bokho1 = json_data.getString("bokho");
bunbo1 = json_data.getString("bunbo");
thoigian.add(thoigian1);
soban.add(soban1);
cafesua.add(cafesua1);
cafeden.add(cafeden1);
cafeda.add(cafeda1);
duatuoi.add(duatuoi1);
nuocngot.add(nuocngot1);
cavienchien.add(cavienchien1);
goiga.add(goiga1);
bokho.add(bokho1);
bunbo.add(bunbo1);
}
}
flag = true;
}
} catch (Exception e) {
// Log.e("log_tag", "Error in http connection" + e.toString());
Toast.makeText(getApplicationContext(), e.toString(),
Toast.LENGTH_LONG).show();
}
return null;
}
}
For handling rotaion of screen in your application you should add the attribute
android:configChangesinside the activity tag as:and override
onConfigurationChangedin code part as to avoid for executing AsyncTask again you will need to use an boolean flag if screen is rotate or not :