不使用 oncreate 在活动之间交换数据

发布于 2024-12-17 05:36:02 字数 296 浏览 3 评论 0原文

我已经实现了一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末 2024-12-24 05:36:02

它崩溃的原因正是因为每次切换 Activity 时都会运行 onCreate。它每次都会启动一个新活动,因此每个活动都有多个实例,并且内存不足。

要阻止这种情况发生,您应该在意图上设置一个标志,例如:

int iflags = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
Intent i = new Intent("com.you.yourapp.yourotheractivity");
i.setFlags(iflags);
// Apply your extras
startActivity(i);

该标志将导致它重用其他活动(如果它在后台),因此 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:

int iflags = Intent.FLAG_ACTIVITY_REORDER_TO_FRONT;
Intent i = new Intent("com.you.yourapp.yourotheractivity");
i.setFlags(iflags);
// Apply your extras
startActivity(i);

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

各空 2024-12-24 05:36:02

android:launchMode 添加到 AndroidManifest.xml 中的主要 Activity 中。根据您的要求使用 singleTasksingleInstance

<activity android:name="com.app.activity" android:launchMode="singleTask" ...>

引自 http://developer.android.com/guide/topics /manifest/activity-element.html#lmode

单任务

系统在新任务的根部创建活动并将意图路由到它。但是,如果该 Activity 的实例已存在,系统将通过调用其 onNewIntent() 方法将 Intent 路由到现有实例,而不是创建一个新实例。

单实例

与“singleTask”相同,只是系统不会在持有实例的任务中启动任何其他活动。该活动始终是其任务的唯一成员。

Add android:launchMode into your main activity in AndroidManifest.xml. Use either singleTask or singleInstance depending of your requirements.

<activity android:name="com.app.activity" android:launchMode="singleTask" ...>

Quotes from http://developer.android.com/guide/topics/manifest/activity-element.html#lmode:

singleTask

The system creates the activity at the root of a new task and routes the intent to it. However, if an instance of the activity already exists, the system routes the intent to existing instance through a call to its onNewIntent() method, rather than creating a new one.

singleInstance

Same as "singleTask", except that the system doesn't launch any other activities into the task holding the instance. The activity is always the single and only member of its task.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文