Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 484
Favolas Avatar asked Aug 29 '12 10:08

Favolas


1 Answers

Use configChanges in your activity under manifest file.

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

This worked for me.

like image 58
naamadheya Avatar answered Sep 18 '22 12:09

naamadheya