0

I have a ListView, populated from a XML. Everything is working fine, but when i change my device's orientation(Portrait<-->Landscape) it reloads the ListView. I want to stop it. I have used onSaveInstanceState and onConfigurationChanged. But it still reloads.

Here is my code:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState != null) {
        alrowDataView = savedInstanceState.getParcelableArrayList("name");
    //  Toast.makeText(MainActivity.this,alrowDataView.size() , Toast.LENGTH_LONG).show();

        // lvrowDataView=(ListView) findViewById(R.id.list_xml);
    }
    else{
        Toast.makeText(MainActivity.this,"NOdata" , Toast.LENGTH_LONG).show();

    setContentView(R.layout.activity_main);
    lvrowDataView = (ListView) findViewById(R.id.list_xml);
    ParsingXml parsing = new ParsingXml();

After that general ListView operations...

and here is the code for OnSavedInstanceState and onConfigurationChanged.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);


    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        Toast.makeText(MainActivity.this, "landscape", Toast.LENGTH_SHORT).show();
    } else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
        Toast.makeText(MainActivity.this, "portrait", Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putParcelableArrayList("name", alrowDataView);
    //Toast.makeText(MainActivity.this, alrowDataView.size(),Toast.LENGTH_LONG).show();
}

and these are the declarations

    private ListView lvrowDataView;
private ArrayList<RowData> alrowDataView;
RowDataAdapter adapterRowData;

I don't want to use android:configChanges="orientation"

Also,in onSaveInstanceState the ArrayListis null!!!

2
  • So, what is your reason for not wanting to use configChanges in your manifest?
    – tolgap
    Sep 25, 2013 at 9:08
  • well firstly it is not working,i have tried it!then it is just a demo!in the real one,there will be some other activity which i need to be refreshed.Thats why.I didn't knew that before. Sep 25, 2013 at 9:12

1 Answer 1

1

Try this way

android:configChanges="orientation"

Is not working with latest android api 3.0+

You need to use like this

<activity
            android:name="YourActivity"
            android:configChanges="keyboardHidden|orientation|screenSize"
 />
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.