Pushpendra Kuntal
Pushpendra Kuntal

Reputation: 6196

Android: unable to change ImageView at run time

I am using ImageView in my example. I set Images as background of imageview in xml. Now I want to change this image background of Imageview at run time.

this is my java code.

 changeImage()

       {
       ImageView imgview=(ImageView)findViewById(R.id.imageView1);
       imgview.setImageResource(R.drawable.headerhindi);
       }

I am calling this method from onCreate(). First time my method works fine. but when I redirect again to my activity onCreate() using startActivity(myActivityIntent); this method does not works properly means Images does not change according to this method. By default images which is set in xml is shown.

Please help me to find out the solution. Thank you in advance.

Upvotes: 0

Views: 573

Answers (2)

oldergod
oldergod

Reputation: 15010

How to you redirect to your activity onCreate ?

If your activity was already running, it skips onCreate and goes directly to the Started state.
You probably want to move your changeImage() to the onStart() method which get called on creation and resuming.

enter image description here

You can see full explanations here: Managing the Activity Lifecycle

※ The main difference for you between onStart() and onResume() would be that onStart() is called before the activity becomes visible while onResume() is called after.

Upvotes: 1

shanet
shanet

Reputation: 7334

Try moving the call to changeImage() to onResume().

I haven't tested it, but I have a feeling that what's happening is that when you call startActivity() to start the activity again, the activity was not destroyed yet so onCreate() is not called again as per the activity lifecycle:

enter image description herehttp://developer.android.com/images/activity_lifecycle.png. Just a hunch though.

Upvotes: 1

Related Questions