Reputation: 11
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!!!
Upvotes: 0
Views: 1198
Reputation: 28484
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"
/>
Upvotes: 1