防止活动在方向更改时重新加载内容

时间:2015-04-24 15:07:01

标签: java android

我希望在旋转屏幕时阻止我的活动重新加载。问题是我使用适配器从我在JSON中收到的链接下载图像。但每次我旋转屏幕时,活动都会继续从服务器下载图像,这导致没有必要的连接。 如何在每次更改方向时阻止我的活动重新启动?

public class SpaActivity extends NetworkActivity {

    ArrayList<Spa> SpaList;
    SpaAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spa);
        SpaList = new ArrayList<Spa>();
        new JSONAsyncTask().execute(GlobalConstants.SPA_INFO_JSON);

        ListView listview = (ListView)findViewById(R.id.list);
        adapter = new SpaAdapter(getApplicationContext(), R.layout.list_row, SpaList);

        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                                    long id) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), SpaList.get(position).getTitle(), Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_spa, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    class JSONAsyncTask extends AsyncTask<String, Void, Boolean> {

        ProgressDialog dialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            dialog = new ProgressDialog(SpaActivity.this);
            dialog.setMessage("Loading, please wait");
            dialog.setTitle("Connecting server");
            dialog.show();
            dialog.setCancelable(false);
        }

        @Override
        protected Boolean doInBackground(String... urls) {
            try {

                //------------------>>
                HttpGet httppost = new HttpGet(urls[0]);
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = httpclient.execute(httppost);

                // StatusLine stat = response.getStatusLine();
                int status = response.getStatusLine().getStatusCode();

                if (status == 200) {
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);

                    JSONObject jsono = new JSONObject(data);
                    JSONArray jarray = jsono.getJSONArray("spas");

                    for (int i = 0; i < jarray.length(); i++) {
                        JSONObject object = jarray.getJSONObject(i);

                        Spa spa = new Spa();

                        spa.setTitle(object.getString("title"));
                        spa.setImage(object.getString("image"));
                        spa.setDuration(object.getInt("duration"));
                        spa.setPrice(object.getDouble("price"));
                        spa.setDescription(object.getString("description"));

                        SpaList.add(spa);
                    }
                    return true;
                }

                //------------------>>

            } catch (ParseException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return false;
        }

        protected void onPostExecute(Boolean result) {
            dialog.cancel();
            adapter.notifyDataSetChanged();
            if(result == false)
                Toast.makeText(getApplicationContext(), "Unable to fetch data from server", Toast.LENGTH_LONG).show();

        }
    }

}

2 个答案:

答案 0 :(得分:0)

将此添加到您不想在AndroidManifest.xml中重新加载的活动中:

android:configChanges="orientation|screenSize"

即:

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

答案 1 :(得分:0)

在清单中添加此行

<activity android:name=".MyActivityName"
android:configChanges="orientation|screensize">
</activity>