2

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?

3
  • maybe fix the problem of them opening each other repetitively first? Commented 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? Commented Jul 12, 2016 at 0:43
  • You should probably be looking to solve this problem using less activities.... Commented Jul 12, 2016 at 1:46

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.