Outofmemoryerror when starting Activity sometimes

Asked

Viewed 64 times

2

I am developing my software engineering project that consists of an Android application and I have a problem in the transition between Activity.

The problem is based on two screens:

Login screen, composed by:

  • Imagen (logo)
  • E-mail field
  • Field Password
  • Register
  • I forgot my password

Password recovery screen, composed by:

  • E-mail field
  • Send

Ok, when I press the Send button from the recovery screen I am redirected to the login screen. That’s the problem, if I do it three times the application stops working.

Flow: Login > Recovery > Login > Recovery > Login > Recovery > Login ERROR

This is the way I’m using to make the transition between an Activity and another. I don’t know if this is the right one, making Activity pause and the other summarize.

Intent intentLogin = new Intent(RecuperarActivity.this, MainActivity.class);
startActivity(intentLogin);

This is the error generated:

java.lang.OutOfMemoryError: Failed to allocate a 24582668 byte allocation    with 16777120 free bytes and 16MB until OOM

It looks like he’s sort of re-allocating Activity. I ran a test and the onResume method is called normally. The other parts of the error talk about Bitmapfactory has relationship to the image that should weigh, but even so I should be able to open a screen as many times as I want.

Thanks in advance!

  • 1

    A possible solution is to finish() after startActivity(). Also see if the image is not larger than necessary

  • 1

    You may have a memory leak. Activity being destroyed does not mean that the trash has been collected. Turn on the Do Not Keep Activities option in the developer options. If you’re giving the problem anyway, that’s certainly it.

  • It worked friends. I tested it first with the feature that Pablo mentioned to be sure and then I gave Finish(). But stay tuned for the next one. If I give Finish in the Login Activity, when I’m on the recovery screen I can’t get back to login if I don’t try to recover an account. For this I used `intentLogin.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); so I eliminate the activity stack when I go back to login.

  • @Joãofelipegonçalves I’m not a fan of finish() to solve this. I think it’s more interesting you try to find the root of the problem. A guess: you have static fields in your Activity?

  • @Pabloalmeida As a label? In this case I have two, "Register" and "I forgot my password"

  • following the @ramaral response of the finish() only on an Activity that you will no longer use, such as your password recovery screen, and the login screen since it is a screen that comes on hold for another Activity to be executed before returning to it you can put it on onResume() (Activity lifecycle), look at the answer to my question http://answall.com/questions/115564/actualizr-listview-usando-notifydatasetchanged/115575#115575 I always return to that screen after a change in the list.

  • If you put a large image or in the wrong folder. you will generate this error. Post stacktrace

Show 2 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.