Hi Stefan,
Well, I think the code in getSaveInstanceState is going to cause a
ClassCastException because you're storing String objects, yet trying
to cast those Strings to SaveHelper and Location respectively. You'll
have to write the non-transient fields from your SaveHelper and
Location objects yourself into the Bundle in onSaveInstanceState and
then reconstruct them in getSaveInstanceState. Maybe a cleaner way to
do this is to have a helper class that has methods like the following
(I haven't compiled or tested this code, it's just to make my point):
public class LocationBundlePersister
{
public static void onSaveInstanceState(Bundle savedInstanceState,
final Location location)
{
// @todo here's where you do the work of writing the things in
Location you need to save to the bundle
}
public static Location getSaveInstanceState(Bundle
savedInstanceState)
{
Location l = null;
// @todo here's where you would construct a Location based on the
data stored in the bundle
return l;
}
}
I see that Bundle lets you read and write an object that implements
Serializable, however Location doesn't implement this interface and
I'm not sure if your SaveHelper class does.
Hope this helps.
- Mike
On Oct 7, 4:37 pm, Stefan <[email protected]> wrote:
> Hello,
>
> i want to save important data. If the user rotate the device, the
> activity will be recreated (or i have to use the
> onConfigurationChanged-method).
> I want to save an instance of a class and some Location variables. I
> can save all integer values of one Location variable, but this is a
> dirty way and i have still the problem with my instance variable.
>
> So i hoped, that the following code would work, but this is not the
> case (save the string representation and get the values back with a
> cast of the string representation):
>
> public void onSaveInstanceState(Bundle savedInstanceState) {
>
> super.onSaveInstanceState(savedInstanceState);
> savedInstanceState.putString("sh", sh.toString());
> savedInstanceState.putString("last", last.toString());
> ......
> }
>
> public void getSaveInstanceState(Bundle savedInstanceState)
> {
> if (savedInstanceState!=null)
> {
> ........
> sh = (SaveHelper)savedInstanceState.get("sh");
> last = (Location)savedInstanceState.get("last");
> .......
> }
>
> }
>
> Or must I use the onRetainNonConfigurationInstance()-function?? But
> there i only get one return value?!?
>
> And if I use the onConfigurationChanged-function and
> android:configChanges="keyboardHidden|orientation" in my
> AndroidManifest.xml, the app will recreate after other configuration
> changes, too....
>
> So what is the best way for me??
>
> Thanks,
> Stefan
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---