0

In my code I start a new activity for street view.

Intent streetView = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(streetView);

It Launches the street view just fine. If I don't do much, I can hit the back button and my App is sitting there waiting like nothing had changed.

However, if I use street view for a bit and look around and move the view around, I see this in the logcat:

01-27 10:53:27.526: INFO/ActivityManager(1079): Low Memory: No more background processes.

01-27 10:53:27.636: DEBUG/dalvikvm(32218): GC_FOR_MALLOC freed 3054 objects / 198608 bytes in 655ms

01-27 10:53:27.831: DEBUG/dalvikvm(32164): GC_FOR_MALLOC freed 1054 objects / 51664 bytes in 342ms

01-27 10:53:27.862: INFO/dalvikvm-heap(32164): Grow heap (frag case) to 3.656MB for 87396-byte allocation

01-27 10:53:27.878: INFO/ActivityManager(1079): Process com.favorite.me (pid 32135) has died.

01-27 10:53:27.878: INFO/ActivityManager(1079): Low Memory: No more background processes.

01-27 10:53:27.482: INFO/ActivityManager(1079): Process com.myapp.app (pid 31481) has died.

01-27 10:53:27.490: INFO/WindowManager(1079): WIN DEATH: Window{44c68ba8 com.myapp.app/com.myapp.app.MyLocation paused=false}

Then when I hit the back button to return to my app, it seems like all the variable have been reset to null, like I've lost my app's state. The only way to fix it is to hit the back button again and relaunch that activity.

From what I can tell, its not calling the onpause() or the onSaveInstanceState() methods and the OS is killing off my App for memory to run streetview.

Is there something I need to do differently?

Here is my oncreate, onRestoreInstanceState and onSaveInstanceState methods:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

        @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
      super.onRestoreInstanceState(savedInstanceState);
        sLong = savedInstanceState.getDouble("sLong");
        sLat = savedInstanceState.getDouble("sLat");
        sMapStyle = savedInstanceState.getBoolean("sMapStyle");
        sMapUpdate = savedInstanceState.getBoolean("sMapUpdate");
        sMapStreet = savedInstanceState.getBoolean("sMapStreet");
        sFirstTime = savedInstanceState.getBoolean("sFirstTime");
        locationAvailable = savedInstanceState.getBoolean("locationAvailable");
        sGPSFlag = savedInstanceState.getBoolean("sGPSFlag");
        sMessage = savedInstanceState.getString("sMessage");
        sMap = savedInstanceState.getString("sMap");
        sGPS = savedInstanceState.getString("sGPS");
        sAddressIn = savedInstanceState.getString("sAddressIn");
        sGPSLat = savedInstanceState.getString("sGPSLat");
        sGPSLong = savedInstanceState.getString("sGPSLong");
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
      super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putDouble("sLong",sLong);
        savedInstanceState.putDouble("sLat",sLat);
        savedInstanceState.putBoolean("sMapStyle",sMapStyle);
        savedInstanceState.putBoolean("sMapUpdate",sMapUpdate);
        savedInstanceState.putBoolean("sMapStreet",sMapStreet);
        savedInstanceState.putBoolean("sFirstTime",sFirstTime);
        savedInstanceState.putBoolean("locationAvailable",locationAvailable);
        savedInstanceState.putBoolean("sGPSFlag",sGPSFlag);
        savedInstanceState.putString("sMessage",sMessage);
        savedInstanceState.putString("sMap",sMap);
        savedInstanceState.putString("sGPS",sGPS);
        savedInstanceState.putString("sAddressIn",sAddressIn);
        savedInstanceState.putString("sGPSLat",sGPSLat);
        savedInstanceState.putString("sGPSLong",sGPSLong);
    }

I think I may have a lead on why this is happening. Still not sure how correct it yet. I set some debugging messages and I am seeing the onRestoreInstanceState() and onSaveInstanceState() methods run.

The problem is, it is not restoring my saved instance until after my onCreate() method finishes. But it is during the onCreate() method that I am needing these variables which are still null at this point. How can I get my code to run after the onCreate() but without requiring any user input? or How can I get the onRestoreInstanceState() to run before my code?

Brian
  • 135
  • 9
  • How do you know that `onpause()` and `onSaveInstanceState()` are not being called? Are you attempting to save your state in one (or both) of these methods? Are you logging the call to them? – dave.c Jan 27 '11 at 16:34
  • From the logcat it tells me the pause = false. So it doesn't look like its running onpause(). Also I put the variables I'm attempting to save in my onSaveInstanceState() method. – Brian Jan 27 '11 at 17:39

3 Answers3

2

You might not be using onSaveInstanceState correctly.

  1. Make sure that you write the variables that represent your app's state to the Bundle passed to you in onSaveInstanceState.

  2. in you activity's onCreate, use the given Bundle to restore your state. Alternatively you can do this in onRestoreInstanceState.

Refer to this stackoverflow question for a full example.

Community
  • 1
  • 1
Lior
  • 7,845
  • 2
  • 34
  • 34
  • Even though I save them in the onSaveInstanceState, it still shows null after I return from Streetview activity when I see in logcat that it has killed my process. – Brian Jan 27 '11 at 17:40
  • I had a brain storm last night while working with the `onRestoreInstanceState()` and I was able to call actually call some of the code I needed in this method, skipping the need for it to be called in `onCreate()` again. Thanks for the suggestion! – Brian Jan 28 '11 at 12:59
1

If your app is being killed to free up resources, then there is nothing you can do to prevent it or even save your instance data before it is killed.

Take a look at Saving Persistent State on the Android dev site. They suggest that it may be necessary to more aggressively save your instance state - this will protect against losing state when your app is killed by Android:

You will probably want to commit your data even more aggressively at key times during your activity's lifecycle: for example before starting a new activity, before finishing your own activity, when the user switches between input fields, etc.

All I can suggest is that you explicitly save your instance state before you launch the street view activity.

If this does not help with your instance state being null in your onCreate and you are sure it was saved correctly, you will probably want to restore your instance state before the rest of your code in onCreate.

Also note from the same link:

For onRestoreInstanceState:

This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState.

Now onStart:

Called after onCreate(Bundle) — or after onRestart() when the activity had been stopped, but is now again being displayed to the user.

So, you cannot restore the state of anything you need in onCreate from your onRestoreInstanceState - you will have to do this in onCreate.

RivieraKid
  • 5,923
  • 4
  • 38
  • 47
0

In onSaveInstanceState function "super.onSaveInstanceState" should be at the end.

WojtekT
  • 4,735
  • 25
  • 37