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

Java 防止活动在方向更改时重新加载内容,java,android,Java,Android,当我旋转屏幕时,我想防止我的活动重新加载。问题是我使用适配器从JSON中接收的链接下载图像。但每次我旋转屏幕时,活动都会不断从服务器下载图像,这导致不必要的连接。 如何防止每次更改方向时都重新启动活动 public class SpaActivity extends NetworkActivity { ArrayList<Spa> SpaList; SpaAdapter adapter; @Override protected void onCre

当我旋转屏幕时,我想防止我的活动重新加载。问题是我使用适配器从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();

        }
    }

}
公共类SpaActivity扩展了NetworkActivity{
阵列列表;
SPA适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spa);
SpaList=newarraylist();
新建JSONAsyncTask().execute(GlobalConstants.SPA_INFO_JSON);
ListView ListView=(ListView)findViewById(R.id.list);
adapter=新SpaAdapter(getApplicationContext(),R.layout.list_行,SpaList);
setAdapter(适配器);
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
公共单击(适配器视图arg0,视图arg1,内部位置,
长id){
//TODO自动生成的方法存根
Toast.makeText(getApplicationContext(),SpaList.get(position).getTitle(),Toast.LENGTH_LONG).show();
}
});
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.menu\u spa,menu);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
类JSONAsyncTask扩展了AsyncTask{
进程对话;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
dialog=新建ProgressDialog(SpaActivity.this);
setMessage(“正在加载,请稍候”);
setTitle(“连接服务器”);
dialog.show();
对话框。可设置可取消(false);
}
@凌驾
受保护的布尔doInBackground(字符串…URL){
试一试{
//------------------>>
HttpGet-httppost=新的HttpGet(URL[0]);
HttpClient HttpClient=新的DefaultHttpClient();
HttpResponse response=httpclient.execute(httppost);
//StatusLine stat=response.getStatusLine();
int status=response.getStatusLine().getStatusCode();
如果(状态==200){
HttpEntity=response.getEntity();
字符串数据=EntityUtils.toString(实体);
JSONObject jsono=新的JSONObject(数据);
JSONArray jarray=jsono.getJSONArray(“spas”);
for(int i=0;i>
}捕获(解析异常e1){
e1.printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}捕获(JSONException e){
e、 printStackTrace();
}
返回false;
}
受保护的void onPostExecute(布尔结果){
dialog.cancel();
adapter.notifyDataSetChanged();
如果(结果==false)
Toast.makeText(getApplicationContext(),“无法从服务器获取数据”,Toast.LENGTH_LONG.show();
}
}
}

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

android:configChanges="orientation|screenSize"
i、 e:


在您的清单中添加此行

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

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