Search code examples
androidlistviewandroid-activityonresume

ListView empty after Activity pause


So I have this Activity:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lista_hoteles);

    listaHoteles = (ListView) findViewById(R.id.listaHoteles);
    hoteles = new Operaciones(getApplicationContext()).getHoteles("");

    new CargarHotels().execute();
}

With this Asynck task:

class CargarHotels extends AsyncTask<Void, Void, Void> {

    protected void onPreExecute() {

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        try {
                hotAdapter = new HotelesAdapter(getApplicationContext(), hoteles);
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {

                        listaHoteles.setAdapter(hotAdapter);

                    }
                });
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}

Them when I enter into this Activity everything goes right, the Listview display a list of hotels and his rating, but whenever I press the home button and go to another app (ie. Whatsapp, Facebook etc) and them go back to my Activity the listview display empty.


Solution

  • Well, one solution would be to have your onResume() activity repopulate the ListView. So just override your onResume() like this:

    @Override
    public void onResume() {
        super.onResume();
        hoteles = new Operaciones(getApplicationContext()).getHoteles("");
        new CargarHotels().execute();
    }