不使用 oncreate 在活动之间交换数据
我已经实现了一个 Android 应用程序,其中包含两个部分(活动)
1-主要活动接收 GP,计算地图上的 X、Y 像素
2- 从 SD 卡加载地图后显示/滚动地图。
两项活动之间的交换每 20 秒通过意图和附加功能(地图上的 X、Y 绘图)进行一次,
所有这些都运行正常。
问题是每次我发送意图时我都会创建一个新的地图,并且在多次交换后应用程序崩溃。
是否可以在不创建新地图的情况下将数据传输到一项活动?或其他解决方案来修改第二个活动的 OnCreate 参数
感谢帮助
I have realized one application with Android which contain two parts (activities)
1- Main activity receive GPs, calculate X,Y pixels on a Map
2- Showing/scrolling map after loading it from SD card.
The exchange between both activities is made every 20s by Intent and extras (X, Y plots on the Map)
All that is working properly.
The problem is each time i send intent I create a new Map and after many exchange the application crashes.
Is it possible to transfert data to one activity without creating new map? or other solution to modify OnCreate parameters of the second activity
Thank for help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它崩溃的原因正是因为每次切换 Activity 时都会运行 onCreate。它每次都会启动一个新活动,因此每个活动都有多个实例,并且内存不足。
要阻止这种情况发生,您应该在意图上设置一个标志,例如:
该标志将导致它重用其他活动(如果它在后台),因此 onCreate() 仅在第一次运行,此后仅运行 onResume()
It's crashing precisely because onCreate is run each time you switch activities. It's starting a new activity each time, so you have multiple instances of each activity and it runs out of memory.
To stop this happening you should set a flag on the intent like:
This flag will cause it to reuse the other activity if it is in the background, so onCreate() is only run the first time, after this only onResume() runs
将
android:launchMode
添加到 AndroidManifest.xml 中的主要 Activity 中。根据您的要求使用singleTask
或singleInstance
。引自 http://developer.android.com/guide/topics /manifest/activity-element.html#lmode:
单任务
单实例
Add
android:launchMode
into your main activity in AndroidManifest.xml. Use eithersingleTask
orsingleInstance
depending of your requirements.Quotes from http://developer.android.com/guide/topics/manifest/activity-element.html#lmode:
singleTask
singleInstance