how to have value of a variable in onCreat() to be used in a different method

65 views Asked by At

i have value of a variable 'position' in my oncreate which i got it from a different activity using getIntent(). now i want to use the value of this position in a different method.

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tatslideshow);
            Intent j = getIntent();
    int position = j.getExtras().getInt("pos");
}

and i want to use position in this method

public void updateUI() {
                   int imgid[] = alltats[position];

.
.
. 
}

now i know i can pass the value by putting parameters in updateUI() like updateUI(position) and use it. but the problem is i also have a subclass in my class where i am calling this method so i cant put parameters in updateUI(). this is the subclass:

class RefreshHandler extends Handler {
    @Override
    public void handleMessage(Message msg) {
        // TODO Auto-generated method stub
        TatSlideShow.this.updateUI();
    }
    }

TatSlideShow is the main class. please tell me the way to achieve it.

2

There are 2 answers

3
sanbhat On BEST ANSWER

You can call getIntent().getExtras().getInt("pos") from updateUI() directly, since updateUI() is a non static method of your Activity.

0
Legendary Genius On

I am afraid you will have to use a parameter along with method as you already know...Because the scope of variables within methods are within the method block itself. Otherwise they will be destroyed once they are out of it.

Otherwise you will have to use the variable position directly within the UpdateUI method like.

public void updateUI() 
{
               position=getIntent().getExtras().getInt("pos");
               int imgid[] = alltats[position];
}