1

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

2 Answers2

0

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

NickT
  • 23,844
  • 11
  • 78
  • 121
  • hello nickT I agree with your analysis of the problem. i modified the program acordingly. Now the map appear only once .but the extrasdata are managed in the Oncreate of the second activity and consequently there are not used. What you suggest ? – Poulachon-daniel Nov 20 '11 at 16:50
  • You can always define a new class with a static member and getter and setter and use that to transfer the data. You can then access it in the second activity's onResume() via the getter having used the setter in the first activity. – NickT Nov 20 '11 at 23:58
0

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.

Jarno Argillander
  • 5,885
  • 2
  • 31
  • 33