1

In my Activity I have two Gallery items one ExpandableListView and for the children of the ExpandableListView I am having a GridView. Program runs perfectly. What I need is to handle orientation change. I know, when orientation change, activity will call onDestroy and after that onCreate. Because of that my ExpandableListView will collapsed even though the user has expanded before he has done an orientation change.

I found a great concept for handling rotation changes in StackOverflow. This is the link. I have tried to implement that way but I have failed.

What I need is to take the same interface that user has used before the orientation change even after the orientation change happen. Please guide me.

Update with the Manifest content

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >

    <activity 
        android:name=".activities.HomeActivity"  
        android:configChanges="keyboardHidden|orientation|screenLayout" >       

        <intent-filter>
            <action android:name="android.intent.action.MAIN" ></action>
            <category android:name="android.intent.category.LAUNCHER" ></category>
        </intent-filter>            
    </activity>

    <activity 
        android:name=".activities.WebViewActivity"
        android:configChanges="keyboardHidden|orientation|screenLayout" >          
    </activity>

</application>
Community
  • 1
  • 1
AnujAroshA
  • 4,623
  • 8
  • 56
  • 99

2 Answers2

1

Handling rotation could be trivial and depends on what you have to react to when the screen rotates. If you have nothing in particular to do, you can always tell Android itself to manage the rotation, and avoid restarting your activity. This could be achieved by declaring in the AndroidManifest.xml android:configChanges="orientation". If you do so, Android will call onConfigurationChanged, that you can always override. If you want your activity to restart when the screen rotates, you have to save the current state. That is, in your case, it could be the current groups and children currently collapsed and re collapse. You can use onSavedInstanceState()/onRestoreInstanceState() to save and restore the states you need.

Edit: if you are targeting API level 13, you have to add the screenSize option to the android:configChanges in conjunction with the orientation option as stated in the documentation:

The screen orientation has changed — the user has rotated the device.

Note: If your application targets API level 13 or higher (as declared by the minSdkVersion and targetSdkVersion attributes), then you should also declare the "screenSize" configuration, because it also changes when a device switches between portrait and landscape orientations.

JDJ
  • 4,298
  • 3
  • 25
  • 44
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • In my AndroidManifest I have declared `android:configChanges="keyboardHidden|orientation|screenLayout`. But it will not help my problem. Yes, I have read the document and got to know I have to save the previous instance as an Object and get it back when the onCreate() loads again. But as in my case what can I declare as the Object because I'm filling two Gallery items, one ExpandableListView with two GridViews. If I am creating an Object to save my last instance what should I save? – AnujAroshA May 18 '12 at 09:10
  • I have not understand. Could you rephrase a bit? Are you telling me that adding android:configChanges="keyboardHidden|orientation|screenLayout" will recreate your activity? Will it restart from the onCreate? – Blackbelt May 18 '12 at 09:13
  • Yes, it is calling from the onCreate() method again. I can see it the Log out put. After your comment I have override the `onConfigurationChanged` method and call just the Log.d() method. It is not even printing in the Log. – AnujAroshA May 18 '12 at 09:26
  • It is not the correct behavior. You add the configChanges option, it have not to call the onCreate() again. Could you edit your question adding your AndroidManifest.xml file? – Blackbelt May 18 '12 at 09:28
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/11412/discussion-between-anuja-piyadigama-and-blackbelt) – AnujAroshA May 18 '12 at 09:40
  • 1
    Overriding default change is bad. The documentation itself advises against it. This forces you to handle all configuration changes yourself. From the docs: `Note: Handling the configuration change yourself can make it much more difficult to use alternative resources, because the system does not automatically apply them for you. This technique should be considered a last resort when you must avoid restarts due to a configuration change and is not recommended for most applications.` [link](http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange) – Riot Goes Woof Sep 01 '15 at 15:28
0

why don't you save the these things in a bundle.

you put the status in a bundle in in the onDestroy function and you read the bundle in the onCreate function.

onDestroy()
{
 Bundle extras = intent.getExtras();
extras.putInt("group", 5);
extras.putInt("item", 23);
}

onCreate()
{
 Bundle extras = getIntent().getExtras();
 int item= extras.getInt("item");
 int group= extras.getInt("group");
}

With the bundle could you save if the ExpandableListView was expanded.

mariomario
  • 660
  • 1
  • 9
  • 29
  • First of all, there is a simp,e syntax error in your code and there StackOverflow guys are not allow me to edit that tiny mistake :-) About your answer, what are you propose me to put in to Bundle ? Getting data back is not the issue. I want to load the same place that user previously stayed before the orientation happen. As an example; before orientation change, user may stay at the 23rd item of the Gallery and open the 5th group in the ExpandableListView. I want to load that same interface. Thank you – AnujAroshA May 18 '12 at 09:17
  • @AnujaPiyadigama edited maybe this is what you need, else I wouldn't know. – mariomario May 18 '12 at 09:25