Android - How can I access a View object instantiated in onCreate in onResume?

20,229

Solution 1

I would make the image button an instance variable, then you can refer to it from both methods if you like. ie. do something like this:

private ImageButton mImageButton = null;

public void onCreate(Bundle savedInstanceState) {
  Log.d(AntengoApplication.LOG_TAG, "BrowsePicture onCreate");
  super.onCreate(savedInstanceState);
  setContentView(R.layout.layout_post);

  mImageButton = (ImageButton) findViewById(R.id.post_image);
  //do something with mImageButton
}

@Override
protected void onResume() {
  super.onResume();
  mImageButton = (ImageButton) findViewById(R.id.post_image);
  mImageButton.setImageURI(selectedImageUri);
}

It's worth bearing in mind though that instance variables are relatively expensive in Android, so it's more efficient to use a local variable within the method if it's only used in one place.

Solution 2

findViewById() does not create the view, it is just finding the already created view. It was created by inflating the layout R.layout.layout_post in the previous line.

You could simply call findViewById() in your onResume() method to get a reference to it in that method, or you could change ib to an instance variable so that it is accessible in methods other than onCreate().

Solution 3

Move the declaration from the method to the class. Assuming selectedImageUri is within scope...

public class MyApp extends Activity {
    ImageButton ib;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.d(AntengoApplication.LOG_TAG, "BrowsePicture onCreate");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_post);

        ib = (ImageButton) findViewById(R.id.post_image);
    }

    @Override
    protected void onResume() {
        super.onResume();
        ib.setImageURI(selectedImageUri);
    }
}
Share:
20,229
Chris
Author by

Chris

Updated on March 28, 2020

Comments

  • Chris
    Chris about 4 years

    In my onCreate() method, I'm instantiating an ImageButton View:

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_post);
    
        final ImageButton ib = (ImageButton) findViewById(R.id.post_image);
    ...
    

    In onResume, I want to be able to change the properties of the ImageButton with something like:

    @Override
    protected void onResume() {
        super.onResume();
        ib.setImageURI(selectedImageUri);
    }
    

    But onResume doesn't have access to the ib ImageButton object. If this were a variable, I'd simple make it a class variable, but Android does not allow you to define View object in the class.

    Any suggestions on how to do this?

  • Chris
    Chris over 13 years
    Yes, this worked. I had incorrectly thought that this would not work in Android since I kept getting a force close before onCreate. Turns out, my error was that I had: ImageButton ib = (ImageButton) findViewById(R.id.post_image); and that leading "ImageButton" was the culprit. Thx.
  • Oliv
    Oliv over 8 years
    Why are instance variables are relatively expensive in Android. I hear of it for the first time, Performance Tips (developer.android.com/training/articles/perf-tips.html) do not mention it...