I am a beginner in android programming, now i am working on an android application which displays the data in a sort order by default and also provides a search facility. For this i have written an activity called search that will search the contents and displays in a listview. My problem is when i rotate the device the onCreate method of the Activity class is called even after overridding the onConfigurationChanged method. I had also done the following changes in the manifest file to invoke the onConfigurationChanged method.
<activity
android:name=".Search"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="sensor" >
</activity>
My Search Activity is as follows :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.out.println("HERE AT ONCREATE");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
System.out.println("HERE AT ON CONFIGURATION CHANGED");
if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
setContentView(R.layout.search_port);
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
setContentView(R.layout.search_land);
}
}
So the output while rotating the device will be :
-
HERE AT ON CONFIGURATION CHANGED
-
HERE AT ONCREATE
I dont want to get the onCreate() method called while rotating the device. Please help me to solve this issue. I am using Android 2.2 SDK with Samsung Galaxy Tab (7 inch display).
What you’re trying to accomplish with the above code can be done much more simply.
If you change your layout files name to be both the same:
search_layout.xmland place them in your project this way :And then modify your Activity this way :
That’s how I have it setup in my Activity and the
onCreate()method is only called once.