Android Question Releasing Memory when Activity finishes

Discussion in 'Android Questions' started by Gary Milne, Mar 3, 2015.

  1. Gary Milne

    Gary Milne Active Member Licensed User

    I'm capturing the user press of the Back key and doing an Activity.Finish. My understanding is that this should release all of the resources, ie screens, layouts, views and anything defined as global that are associated with that Activity. Is this a correct assumption or should I explicitly do an Activity.RemoveAllViews first, and then call Activity.Finish?

    Is there anything else I should explicitly be doing to free up resources?

    If a "memory leak" existed within an Activity would these steps release that memory?

    Thanks for any answers.
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Normally you don't need to call Activity.RemoveAllViews. The activity will be released with all the views. Unless there is another live reference to the activity or the views.
     
  3. Gary Milne

    Gary Milne Active Member Licensed User

    Can you give an example of how can one Activity can have a live reference to a view in another activity? I don't see how that situation arises.
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    The compiler tries to protect against such cases. However there are ways to pass a reference of a view from one activity to another. For example if you use CallSubDelayed2 and pass the view as a parameter.
     
  5. Gary Milne

    Gary Milne Active Member Licensed User

    Yes, of course. That makes perfect sense. Thanks for the clarification.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice