title: How to avoid the black screen while loading data in android
body: <p>I am fetching data from remote server in android. While the data loads it shows a black screen which remains for 7-8 seconds. This sometimes causes the android to display ANR dialog. I have used asynctask for the background loading of data but the black screen remains. Also I can't see the indeterminate progress in activity bar. What am I doing wrong? I am pasting the code below -</p>
  
  <pre><code>public class ItemActivity extends Activity {
  
      private String str = null;
      private ListView listview;
      private LazyItemLoadAdapter adapter;
      private Item[] item_data = null;
  
      private Item[] filteredItems = null;
  
      private int id;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
          setContentView(R.layout.activity_item);
  
          ActionBarUtils.setActionBar(this);
  
          id = getIntent().getExtras().getInt("id");
  
          listview = (ListView) findViewById(R.id.listViewAllItems);
  
          AsyncData data=new AsyncData();
          data.execute(String.valueOf(id), "http://kurdshopping.net/apj/adlist.php");
          try {
              item_data=data.get();
          } catch (InterruptedException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (ExecutionException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          if (item_data == null || item_data.length == 0) {
              listview.setVisibility(View.GONE);
              return;
          }
          adapter = new LazyItemLoadAdapter(this, R.layout.listview_item_row, item_data);
  
          listview.setAdapter(adapter);
          listview.setOnItemClickListener(new OnItemClickListener() {
  
              @Override
              public void onItemClick(AdapterView&lt;?&gt; arg0, View arg1,
                      int position, long arg3) {
                  int itemid = item_data[position].id;
                  Toast.makeText(ItemActivity.this, String.valueOf(itemid),
                          Toast.LENGTH_SHORT).show();
                  if (id == 96) {
                      Intent carIntent = new Intent(ItemActivity.this,
                              CarActivity.class);
                      carIntent.putExtra("id", itemid);
                      startActivity(carIntent);
                  } else {
                      Intent adIntent = new Intent(ItemActivity.this,
                              AdActivity.class);
                      adIntent.putExtra("id", itemid);
                      startActivity(adIntent);
                  }
              }
          });
  
      }
  
      @Override
      public boolean onPrepareOptionsMenu(Menu menu) {
          if (item_data == null || item_data.length == 0) {
              menu.getItem(0).setEnabled(false);
          }
          return super.onPrepareOptionsMenu(menu);
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.item, menu);
          return true;
      }
  
  
      @Override
      public boolean onMenuItemSelected(int featureId, MenuItem item) {
          switch (item.getItemId()) {
          case R.id.action_search:
              Intent optionsIntent = new Intent(ItemActivity.this,
                      OptionsActivity.class);
              optionsIntent.putExtra("id", id);
              startActivityForResult(optionsIntent, 1);
              break;
          }
          return super.onMenuItemSelected(featureId, item);
      }
  
      private class AsyncData extends AsyncTask&lt;String, Void, Item[]&gt;{
  
          @Override
          protected void onPreExecute() {
              // TODO Auto-generated method stub
              super.onPreExecute();
              setProgressBarIndeterminateVisibility(true);
          }
  
          @Override
          protected Item[] doInBackground(String... arg0) {
              Item[] item_data=null;
              try {
                  ArrayList&lt;NameValuePair&gt; postParameters = new ArrayList&lt;NameValuePair&gt;();
                  postParameters
                          .add(new BasicNameValuePair("id", arg0[0]));
                  str = CustomHttpClient.executeHttpPost(
                          arg0[1], postParameters);
                  JSONArray array = new JSONArray(str);
                  item_data = new Item[array.length()];
  
                  for (int i = 0; i &lt; item_data.length; i++) {
                      JSONObject jdata = array.getJSONObject(i);
                      String path = "http://kurdshopping.net/thumbs/"
                              + jdata.getString("name");
                      int itemid = jdata.getInt("id");
                      item_data[i] = new Item();
                      item_data[i].id = itemid;
                      item_data[i].imageUrl = path;
                      item_data[i].city = jdata.getString("city_name");
                      item_data[i].make = jdata.getString("cc_name");
                      item_data[i].model = jdata.getString("cm_name");
                      item_data[i].price = jdata.getString("price");
                      item_data[i].text = jdata.getString("title") + "\nPrice: "
                              + jdata.getString("price");
                      Log.i("break", "break");
                  }
              } catch (NotFoundException n) {
                  Log.e("ItemActivity", n.getMessage());
              } catch (JSONException j) {
                  Log.e("ItemActivity", j.getMessage());
              } catch (Exception e) {
                  Log.e("ItemActivity", e.getMessage());
              }
              return item_data;
          }
  
          @Override
          protected void onPostExecute(Item[] result) {
              // TODO Auto-generated method stub
              super.onPostExecute(result);
              setProgressBarIndeterminateVisibility(false);
          }
  
      }
  }
  </code></pre>
  
  <p>Any help is appreciated. Thanks in advance.</p>