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?
The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity.
As onCreate() of an Activity is called only once, this is the point where most initialization should go: calling setContentView(int) to inflate the activity's UI, using findViewById to programmatically interact with widgets in the UI, calling managedQuery(android.
Q 9 – What is the purpose of super. onCreate() in android? The super. onCreate() will create the graphical window for subclasses and place at onCreate() method.
onResume() will never be called before onCreate() . Save this answer. Show activity on this post. onResume() will always be called when the activity goes into foreground, but it will never be executed before onCreate() .
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With