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 8903905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T01:59:14+00:00 2026-06-15T01:59:14+00:00

I’ve ton a ton of Google searches on static objects in Java and I

  • 0

I’ve ton a ton of Google searches on static objects in Java and I think I understand how they work in Android. Static objects are GC’ed when the process of the app is terminated, not when the Activity that declares the static object is destroyed (correct me if I’m wrong). I have an app with a Tabhost that uses Fragments in it. The Fragment that declares the object is all the way to the right (there are three Fragments) of the ViewPager. The tab on the right has a ListView that displays user data and I would like that data to persist when the user swipes through all of the tabs or navigates away from the app. This data does not need to be saved, it just needs to remain in memory through normal use of the app. The fix that gives me the result I want is by setting a static prefix to my Array Adapter. My question is, is this considered good practice? I know that using static objects incorrectly can lead to memory issues however I didn’t get any FC’s with extensive use (landscape to portrait over and over, swiping quickly through the tabs, adding a lot of data to the ListView etc.). Here is the code that I am using

static ArrayAdapter<String> arrayAdapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) 
{

    View view = inflater.inflate(R.layout.fragment,
            container, false);      

        if (personList == null)
        {

            personList = new ArrayList<String>();

        }


        if (arrayAdapter == null)
        {

            arrayAdapter = new ArrayAdapter<String>
            (getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, personList);

        }

EDIT:

This is how I am implementing this idea. I have this code in my main Activity (the one that parents the tab fragments. I put this code in the onDestroy() method of the activity to try and reclaim some memory.

@Override
protected void onDestroy() {
    super.onDestroy();

    if (arrayAdapter != null)
    {

        arrayAdapter.clear();

        arrayAdapter = null;

        Log.i("Activity", "Adapter nulled!");

    }


    Log.i("Activity", "Activityhas been destroyed");
}

public ArrayAdapter<String> getArrayAdapter(ArrayList<String> personList)
{

    arrayAdapter = new ArrayAdapter<String>
    (getBaseContext(), android.R.layout.simple_list_item_1, personList);

    Log.i("Activity", "Adapter Created");


    return arrayAdapter;
}

With this code in my Fragment

if (personList == null)
{

    personList = new ArrayList<String>();

}


if (arrayAdapter == null)
{

    arrayAdapter = ((ActivityMainScreen)getActivity()).getArrayAdapter(personList);

}
  • 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-15T01:59:16+00:00Added an answer on June 15, 2026 at 1:59 am

    Remember! Anything that consumes reference to your activity Context and is marked as STATIC is prone to memory leaks. It is highly not recommended. You may use some sort of Observer/Observable pattern to keep your data available in other classes or simple access the required objects via public methods defined in your TabHost activity.

    For more info, read this http://developer.android.com/training/basics/fragments/communicating.html

    Regarding memory leaks, perhaps you should test your current implementation on a low-end device because new model devices are pretty rich in terms of resources.


    Your Activity:

    private ArrayAdapter<String> arrayAdapter = null;
    
    //initialize your adapter in onCreate
    
    public ArrayAdapter<String> getListAdapter(){
        return arrayAdapter;
    }
    

    Your Fragment:

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    
        ArrayAdapter<String> adapter = ((YourActivityClass) getActivity()).getListAdapter();
        yourListView.setAdapter(adapter);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I am using Paperclip to handle profile photo uploads in my app. They upload
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have been unable to fix a problem with Java Unicode and encoding. The
I am writing an app with both english and french support. The app requests

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.