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

  • Home
  • SEARCH
  • 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 1114377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T02:59:50+00:00 2026-05-17T02:59:50+00:00

Just a random question. I’m learning a bit of Android right now, and in

  • 0

Just a random question. I’m learning a bit of Android right now, and in most examples, it seems a lot of common items (such as buttons, editboxes etc) get requested within each function using (cast) findViewById().

Is it considered good or bad practice to store the result of that in an Activity’s member values? Simple example:

public class MyActivity extends Activity {
    private EditText myText;
    public void onCreate(blah blah) {
       // blah
       this.myText = (EditText) findViewById(R.id.mytext);
    }
}

and use the myText field from there on. I think it’d be good for performance (depending on findViewById’s inner workings, I’m quite sure it’s already very fast), but I haven’t seen it be encouraged yet. Also, it wouldn’t be the first time I encountered a situation where ‘caching’ something like this leads to problems (had a case where database connections weren’t released properly because I remembered a ConnectionManager or something in that fashion).

Secondly, somewhat related, if you want to remember something across methods in your Activity (and later on too, when the Activity is restarted later), is it wiser to keep up both a class field and a value in SharedPreferences, or would calling SharedPreferences each time for setting / getting the value where it’s needing be a better solution? (better being ‘cleaner, without impacting performance significantly)

  • 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-05-17T02:59:50+00:00Added an answer on May 17, 2026 at 2:59 am

    That is completely normal practice and is exactly what you SHOULD be doing. If you’re worried about memory leaks, or holding references or whatever, don’t be concerned about that when dealing with views.

    However, you SHOULD be careful about holding references to other contexts because that COULD cause a memory leak. That doesn’t mean you shouldn’t do it, just be careful about when you’re doing it.

    is it wiser to keep up both a class field and a value in SharedPreferences, or would calling SharedPreferences each time for setting / getting the value where it's needing be a better solution?

    You should do both. You should keep a member variable for when you only need to read the data, just be sure that when you WRITE to the member variable, you also change the shared preference.

    Also, it wouldn't be the first time I encountered a situation where 'caching' something like this leads to problems (had a case where database connections weren't released properly because I remembered a ConnectionManager or something in that fashion).

    This is what I was saying first. It all depends on what you’re storing. Views are fine to store, contexts can be dangerous, and database connections and registered listeners can cause really weird bugs. It all depends on the specific case.

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

Sidebar

Related Questions

Just a random question but does anyone know the formula for how a timestamp
OK, bit of a random question, but the best way to do this is
just a really random question but is the property Math.PI in javascript always 3.141592653589793
just a quick question: I am a CS undergrad and have only had experience
Just found this out, so i am answering my own question :) Use a
Another random question that hit me (I've drank ~9 cups of coffee in the
I have a random question about const correctness. Lets say i have a class
Hello i have a question on picking random entries from a database. I have
A traditional C++ class (just some random declarations) might resemble the following: class Foo
I am just starting out in XNA and have a question about rotation. When

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.