I have 8 different activities that open each other on a button click randomly. After clicking the button that opens a new activity around 15 times, it crashes. Do I need to close the activities or is there another solution?
-
maybe fix the problem of them opening each other repetitively first?3kings– 3kings2016-07-12 00:36:30 +00:00Commented Jul 12, 2016 at 0:36
-
I want it to do that. I want a continuous cycle. I'm looking for something akin to a music player on shuffle and repeat all, except with activities. Or should I be looking to accomplish all of this in a single activity?Ethan Watson– Ethan Watson2016-07-12 00:43:51 +00:00Commented Jul 12, 2016 at 0:43
-
You should probably be looking to solve this problem using less activities....FishStix– FishStix2016-07-12 01:46:23 +00:00Commented Jul 12, 2016 at 1:46
3 Answers
As Harsh Pandey said, you may want to look into using fragments instead of activities. Another possible way is to call finish(); right after you start the next activity. This will close the previous activity. However, if you ever intend to go back to this activity using a back button or something, it will not exist.
Comments
When you create new activities and close them, GC will automatically clean up the memory used by the old activities when required. You are getting an out of memory error because,
- Your activities are holding up a reference which is still alive. This will prevent GC from cleaning up the old activities.
for example,
- Look for animations which are not cleared properly.
The best way to deal with this is find the memory leak.
https://developer.android.com/studio/profile/investigate-ram.html
Suggestion:
As 3kings mentined, try to do in one activity. ie Use Fragments !!
Comments
You can request more heap size by adding this in your manifest:
android:largeHeap="true"
However, based on your comment, my guess is that this case is ideally suited for fragments, instead of activities. You can accomplish all of that in a single activity, with multiple reusable fragments.