Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8247289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:47:46+00:00 2026-06-07T22:47:46+00:00

I’m using the Fragment compatibility package. In my Fragment in onAttach, I keep a

  • 0

I’m using the Fragment compatibility package.

In my Fragment in onAttach, I keep a Context reference.

public void onAttach(Activity activity) {
    super.onAttach(activity);
    Log.w(logTag, "Activity is: " + activity);
    mContext = activity;
    Log.w(logTag, "mContext is: " + mContext); // <-- Breakpoint here
}

later on, I use the context

private String loadExampleSuccessXML () {
    try {
        AssetManager assets = this.mContext.getAssets(); // <-- Breakpoint here
        //Other Stuff

When I change orientation, onAttach seems to store the new Context but when I get to loadExampleSuccessXML, mContext is null.

I have breakpoints after mContext has been saved in onAttach, and at mContext.getAsssets().

When I first run the app the debugger shows the value of mContext:

In onAttach(), mContext [MyActivity] (id=830010419632)

In loadExampleSuccessXML(), mContext [MyActivity] (id=830010419632)

But then after a config change

In onAttach(), mContext [MyActivity] (id=830010565472)

In loadExampleSuccessXML(), mContext null

I can’t understand why. Any help would be great.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:47:49+00:00Added an answer on June 7, 2026 at 10:47 pm

    Could you provide some more detail as to why you need to maintain a reference to the context? This has never been a requirement for our apps at least and has been shown (albeit not in your case from the code you have posted) to be the prime culprit in Android memory leaks…

    If you need to maintain an aspect of a previous activity’s state then I would suggest using onSavedInstanceState(). Here you can pass through some simple properties (like the id of an item I last selected in a list). The Manifest config fix is usually the wrong approach despite it’s prevalence on help sites such as this. You probably don’t want that.

    Finally, look into using setRetainInstance(true)…again, in our experience this is quite the dangerous method if used incorrectly! That’s probably just us but it does have some bugs with the recent android.support.v4.* libraries at least. When set to true this ensures that the fragment is never destroyed (i.e. onDestroy() is called) but is simply attached and detached as the owing activity is destroyed on rotation and recreated. It will sort your problem but do have a read of the documentation around it’s usage…some of the side affects are quite subtle.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am reading a book about Javascript and jQuery and using one of the
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.