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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T20:13:48+00:00 2026-06-12T20:13:48+00:00

Every once in a while, entering in a new Activity requires updating values in

  • 0

Every once in a while, entering in a new Activity requires updating values in let’s say several TextViews. So let’s say I got n Strings to write in n TextViews of the starting Activity.

Which one is the best approach to guarantee good performance and providing “clean code”?

Variant 1 (The way I actually apply):
I declare a single TextView variable “tempText” as global variable and assign the TextView to update to this variable (alternatively in a extra method).
Alternatively, a) is doing the whole procedere in the onCreate(), while b) is handle everything in a method called e.g. updateTextViews()

(...)

public class MyActivity extends Activity{

    private TextView tempText;

    public onCreate(Bundle icicle){
        (...)

        tempText = (TextView) findViewById(R.id.tv_1);
        tempText.setText(string_1);

        tempText = (TextView) findViewById(R.id.tv_2);
        tempText.setText(string_2);

        (...)

        tempText = (TextView) findViewById(R.id.tv_n);
        tempText.setText(string_n);
    }
}

Variant 2 :
I declare a single TextView variable “tempText” as variable in the onCreate() or the respective method and assign the TextView to update to this variable.
The rest is in analogy to Variant 1.

(...)

public class MyActivity extends Activity{

    public onCreate(Bundle icicle){
        (...)

        private TextView tempText;

        tempText = (TextView) findViewById(R.id.tv_1);
        tempText.setText(string_1);

        tempText = (TextView) findViewById(R.id.tv_2);
        tempText.setText(string_2);

        (...)

        tempText = (TextView) findViewById(R.id.tv_n);
        tempText.setText(string_n);
    }
}

Variant 3:
I declare a global TextView variable for every TextView to update. This, as far as I know, needs more space in the RAM, but I don’t know about the impact to velocity. Also here, are there differences between handling it in the onCreate() (a)) or in a seperate method (b))?

(...)

public class MyActivity extends Activity{

    private TextView tempText_1;
    private TextView tempText_2;
    (...)
    private TextView tempText_n;

    public onCreate(Bundle icicle){
        (...)

        tempText_1 = (TextView) findViewById(R.id.tv_1);
        tempText_1.setText(string_1);

        tempText_2 = (TextView) findViewById(R.id.tv_2);
        tempText_2.setText(string_2);

        (...)

        tempText_n = (TextView) findViewById(R.id.tv_n);
        tempText_n.setText(string_n);
    }
}

Variant 4:
I declare a TextView variable for every TextView to update in the onCreate() or the respective method which handles this. The rest is in analogy to Variant 3?

(...)

public class MyActivity extends Activity{


    public onCreate(Bundle icicle){
        (...)

        private TextView tempText_1;
        private TextView tempText_2;
        (...)
        private TextView tempText_n;

        tempText_1 = (TextView) findViewById(R.id.tv_1);
        tempText_1.setText(string_1);

        tempText_2 = (TextView) findViewById(R.id.tv_2);
        tempText_2.setText(string_2);

        (...)

        tempText_n = (TextView) findViewById(R.id.tv_n);
        tempText_n.setText(string_n);
    }
}

Which one is the “best” method? Variants 1 and 2 provide reserving only one memory address in the RAM and use this, while according to Robert C. Martins “Clean Code” the variables are really ambiguous. Option 3 and 4 would be the exact opposite. But for the rest I’m not very conscious of other effects.

  • 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-12T20:13:50+00:00Added an answer on June 12, 2026 at 8:13 pm

    Personally, I use a Variant 3 if I need to access the TextViews again elsewhere in Activity (i.e. in onResume, which is where I usually put my static UI updates). If the setText is a one-shot thing, I do something like —

    ((TextView) findViewById( R.id.tv_1 ) ).setText( string_1 );
    ((TextView) findViewById( R.id.tv_2 ) ).setText( string_2 );
    

    If there’s any doubt that the view might not be there (i.e. when you’re modifying layouts during dev), I’d use a wrapper such as —

    private void safeSetText( int id, String caption ) {
        TextView tv = (TextView) findViewById( id );
        if( tv != null )
            tv.setText( caption );
        else
            Log.d( "..... " );
    }
    

    And then in onCreate:
    safeSetText( R.id.tv_1, string_1 );
    safeSetText( R.id.tv_2, string_2 );

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

Sidebar

Related Questions

Every once in a while I try to add a new data model version,
I have Rails app, and every once in a while, when I bring new
I got a problem. Every once in a while, the IntentService UpdateService02 fails to
Every once in a while I want to replace all instances of values like:
Every once in a while I get a entry in my stats that say
Every once and a while I get this error in IE when making an
Im using Visual studio 2008. Every once in a while files that exist in
In this very simple code example, messages get lost every once in a while.
I have a web scraping script that gets new data once every minute, but
I'm using the GLUTesselator and every once in a while EndContour() fails so I

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.