Favolas
Favolas

Reputation: 7243

Android listview disappears after screen rotation

On my layout, I have some buttons tho make some choices and then one button to perform a query to a database. The result of this query is shown in a ListView inside this layout.

The problem is if after I perform the query I rotate the screen, the ListView disappears and have to perform the query again.

I believe that this is happening because the activity restarts. Following the suggestions here I've added to my activity in the manifest android:configChanges="orientation|keyboardHidden" and in my code added:

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      setContentView(R.layout.mylayout);

    }

But this is not working.

Here is the complete code of my activity:

public class MyClass extends ListActivity implements OnClickListener, OnCheckedChangeListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mylayout);

        // Creates the buttons and setOnClickListener and setOnCheckedChangeListener
    }

    @Override
    public void onClick(View v) {

        // Manages the buttons and their functions

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        // See what group in radio group is checked
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // After pressing one button, a query is made and a listview is shown. 
        // This it to handle the user choice after he clicks an item on the listview

    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
      super.onConfigurationChanged(newConfig);
      setContentView(R.layout.mylayout);

    }
}

This is strange because I have some other activity like this:

public class AtoZ extends ListActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.atoz);



    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);

    }

}   

This also performs a query to the database, shows it on a ListView and then handles the user choice. If I rotate the screen, ListView still shows.

What can I do?

Upvotes: 10

Views: 17753

Answers (5)

naamadheya
naamadheya

Reputation: 2542

Use configChanges in your activity under manifest file.

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

This worked for me.

Upvotes: 9

mabg
mabg

Reputation: 2090

You must to override onSaveInstanceState and onRestoreInstanceState

On the onSaveInstanceState save the ListView list on the Bundle.

On the onRestoreInstanceState, restore the list with the Bundle and recreate the adapter.

private ArrayList<String> data;

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putSerializable("d", data);
    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    if (savedInstanceState != null) {
        data = (ArrayList<String>) savedInstanceState.getSerializable("d");
        ListViewAdapter adapter = new ListViewAdapter(this, data);
        ListView listView = (ListView) findViewById(R.id.layout_listview);
        listView.setAdapter(adapter);
    }
    super.onRestoreInstanceState(savedInstanceState);
}

Upvotes: 2

Kelvin Trinh
Kelvin Trinh

Reputation: 1248

There are 2 things can solve your problems:

Solver A:

  1. Add android:configChanges="orientation|keyboardHidden" in your manifest xml file.
  2. REMOVE onConfigurationChanged(Configuration newConfig) function.

Because onCreate() is called only once every time your Activity is initialized. While your onConfigurationChanged() reloads and inflates the new view R.layout.mylayout with empty data => your list will not bound with data.

Solver B:

Move your data binding code which had been written in onCreate() to the overriding method onStart() or onResume(). I recommend that you use onStart() for your case of data binding.

You should see the Develop page Android Activity life cycle

(Update):

Because you load your listContent when clicking a search button on list item, so you have to maintain the data within activity to rebuild the list from it (for e.g: a string of search). Then in the onStart() you rebuild your list from this data.

However, it is quite strange of your logic when clicking a List's item to change the whole List itself. And notice that: onListItemClick(..) is also fired when a button on list item is clicked.

Upvotes: 5

Sameer
Sameer

Reputation: 4389

The ListView gets recreated when you do setContentView in the onConfigurationChanged method. You need to load the data into the ListView again. if you are binding the ListView to an adapter, you need to do it in onConfigurationChanged.

Upvotes: 3

Anders Metnik
Anders Metnik

Reputation: 6237

You will have to redo the query (if you don't save it) when you do an orientation change, because when this is done, oncreate is called again.

You could try and store the data which is in your listview, in something like shared preferences.

Upvotes: -1

Related Questions