主应用程序在启动街景活动后被杀死
在我的代码中,我启动了一项新的街景活动。
Intent streetView = new Intent(android.content.Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(streetView);
它可以很好地启动街景。如果我不做太多事情,我可以点击后退按钮,我的应用程序就会坐在那里等待,就像什么都没有改变一样。
但是,如果我使用街景视图并环顾四周并移动视图,我会在 logcat 中看到以下内容:
01-27 10:53:27.526: INFO/ActivityManager(1079): Low Memory: No more后台进程。
01-27 10:53:27.636:DEBUG / dalvikvm(32218):GC_FOR_MALLOC在655毫秒内释放了3054个对象/ 198608字节
01-27 10:53:27.831:DEBUG / dalvikvm(32164):GC_FOR_MALLOC释放了1054个对象/ 342 毫秒内 51664 字节
01-27 10:53:27.862:INFO / dalvikvm-heap(32164):将堆(碎片情况)增长到3.656MB,用于87396字节分配
01-27 10:53:27.878:INFO / ActivityManager(1079):处理com .favorite.me (pid 32135) 已死亡。
01-27 10:53:27.878:INFO/ActivityManager(1079):内存不足:不再有后台进程。
01-27 10:53:27.482: INFO/ActivityManager(1079): 进程 com.myapp.app (pid 31481) 已死亡。
01-27 10:53:27.490: INFO/WindowManager(1079): WIN DEATH: Window{44c68ba8 com.myapp.app/com.myapp.app.MyLocation Paused=false}
然后,当我按后退按钮返回到我的app,似乎所有变量都已重置为 null,就像我丢失了应用程序的状态一样。解决此问题的唯一方法是再次单击后退按钮并重新启动该活动。
据我所知,它没有调用 onpause() 或 onSaveInstanceState() 方法,并且操作系统正在杀死我的应用程序以获取运行街景的内存。
我需要做些什么不同的事情吗?
这是我的 oncreate、onRestoreInstanceState 和 onSaveInstanceState 方法:
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);
}
我想我可能知道为什么会发生这种情况。仍然不确定如何正确。 我设置了一些调试消息,并且看到 onRestoreInstanceState()
和 onSaveInstanceState()
方法运行。
问题是,直到我的 onCreate() 方法完成之后,它才会恢复我保存的实例。但正是在 onCreate()
方法期间,我需要这些变量,这些变量此时仍为 null。如何让我的代码在 onCreate()
之后运行,但不需要任何用户输入?或者如何让 onRestoreInstanceState()
在我的代码之前运行?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可能没有正确使用
onSaveInstanceState
。确保将代表应用状态的变量写入在
onSaveInstanceState
中传递给您的 Bundle。在您的 Activity 的
onCreate
中,使用给定的 Bundle 来恢复您的状态。或者,您可以在onRestoreInstanceState
中执行此操作。有关完整示例,请参阅此 stackoverflow 问题。
You might not be using
onSaveInstanceState
correctly.Make sure that you write the variables that represent your app's state to the Bundle passed to you in
onSaveInstanceState
.in you activity's
onCreate
, use the given Bundle to restore your state. Alternatively you can do this inonRestoreInstanceState
.Refer to this stackoverflow question for a full example.
如果您的应用程序被终止以释放资源,那么您无法采取任何措施来阻止它,甚至无法在其被终止之前保存实例数据。
查看 Android 开发网站上的保存持久状态。他们建议可能有必要更积极地保存您的实例状态 - 这将防止您的应用程序被 Android 杀死时丢失状态:
是,您在启动街景活动之前明确保存实例状态。
如果这对 onCreate 中的实例状态为 null 没有帮助,并且您确定它已正确保存,则您可能需要在 onCreate 中的其余代码之前恢复实例状态。
另请注意同一链接:
对于 onRestoreInstanceState:
现在开始:
因此,您无法从 onRestoreInstanceState 恢复 onCreate 中所需的任何状态 - 您必须在 onCreate 中执行此操作。
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:
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:
Now onStart:
So, you cannot restore the state of anything you need in onCreate from your onRestoreInstanceState - you will have to do this in onCreate.
在 onSaveInstanceState 函数中,“super.onSaveInstanceState”应该位于末尾。
In onSaveInstanceState function "super.onSaveInstanceState" should be at the end.