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.
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:
http://developer.android.com/images/activity_lifecycle.png. Just a hunch though.
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.
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.
Related
Is it necessary to initialize all the views of activity in onCreate() only. Can you tell me best initializations of views of activity.
Thanks
No need to initialize any view if you do not modify it.
you can initialize in any Activity lifecycle as your wish(before accessing).
But is is said as best practice to initialize it in onCreate().
Why:
if you see the lifecycle OnCreate is called when you app page is not shown. Like onStart called when app is partially visible & onResume is called when it is fully visible. So, mostly we want everything ready before seeing it. So that is one reason.
Another is findViewById is bit more expensive. So, we don't want to see that when app is visible.
OnStart & onResume may call multiple time when you go another page. So, it's preferable to initialize everything only once , than multiple time.
So, choice is yours now.
OnCreate() : This method is called once when activity is to be created. That is why all the gobal and static content should go there. Example - This may include your shared preferences, databases initialisations.
OnStart() : This method is called when you see your activity over screen. It is foreground method. OnStart() ends with OnStop(). Example : Let us assume A and B activity, A activity has been created and currently in onStart() method is being called. When one switches to B activity then A's OnStop() method will be called and B activity will be created. Thus OnStart() and OnStop() methods are called when you switches onto activities.
So according to your question initialisation is done once so it should be done in OnCreate() method if it is done in OnStart() then initialisation will takes place every time when you switch between activities.
Source : Difference between onCreate() and onStart()?
Please take a look over here this will clear your all faults regarding life cycle Activity | Android Developer
I am a newbie to android app development.So I am having confusion with onActivityResult() method.
In onCreate() method I made a button onClickListener which on clicked will call startActivityForResult for selection of images from gallery.
What does exactly happen after onClickListener is called.Does my previous activity restarts again by calling onCreate() method or resumes by calling onResume() method.But what I have seen is that still onCreate() method is responding while clicking button and taking me to gallery.
What is the exact order of execution of this one with activity life cycle?
In this case, onRestart() -> onStart() -> onActivityResult() -> onResume() is what happens after choosing a picture from the gallery.
An activity only needs to call onCreate() and assign onClickListeners once per life cycle.
Overide all lifecycle methods (like onStart, onStop...) and add yours log if you want to know what happens with your Activity
something like that:
onStart(){
super.onStart();
Log.i("MyTag","onStart");
}
and look at LogCat with tag:MyTag :)
also look at this https://github.com/xxv/android-lifecycle
The only difference from calling startActivity vs startActivityForResult is that when your first activity is resumed, you will get a callback to onActivityResult (in case the second activity called explicitly to setResult()).
Regarding the lifecycle, after going back from your second activity (by finishing the image selection or clicking back button), your first activity will be resumed and not created again. You can review Activities lifecycle in Android documentation Activity Lifecycle
I am getting an issue when i try to open my activity. Actually the state is i am calling an api when i open the activity but when i minimize the application and open it again the home page is opening but the api is not calling again,so i have the doubt that how could run keep the same state when i will open the page the api should call.
I am calling the api inside oncreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//here i am calling the api
}
Could somebody help me #thanks
Try putting your code in onResume() method.
It is guaranteed to be called when the activity is shown.
Needless to say that you will have to override onResume() method as well :)
onCreate() is called when your activiy is creating. If you want to do work when activity is shown, you must do it in onResume()
For detailed information and lifecycle of activites: Activity
This explains the entire lifecycle
onCreate is only called for the first time when activity is created. Once you, minimize and again launch the application, onCreate wont be called. You can call your api from different place based on your need.
I think in your case you can call in the "onWindowFocusChanged (boolean hasFocus)". This function is always called when window gains or looses focus. based on "hasFocus" value you can call the appi. You also may use onResume() which will be called when you activity resumes..
onWindowFocusChanged (boolean hasFocus)" is the best way to do it..
When I exit my app (by pressing back or the home button) the Activitys onDestroy() method is called (where I do lots of clean up with bitmaps).
When I reopen the app, onCreate() does not get called... it goes straight to onStart(), despite the fact that the Activity was finished. This is causing a "trying to use a recycled bitmap" error.
Is there a way to ensure that onCreate() is always called after an Activity is destroyed?
EDIT: I was mistaken. onCreate() IS being called. However, I am still getting the "trying to use a recycled bitmap" error. If onCreate() is going through all of it's steps, wouldn't any recycled bitmaps be reloaded?
Your app must be doing something to forcefully ensure that onDestroy gets called, because if you look at the Activity lifecycle there's no path to get back to onStart from onDestroy that doesn't include onCreate. In reality, an Activity unwinds its initialization with reverse callbacks to the ones that bring it into the resumed state. Take a look at the official documentation here Perhaps you're calling the finish() method somewhere to force quite the Activity?
when you press the home button , the activity is not destroyed , it is just sent to background , and the method onPause() is called, and when you launch it again , the method onResume() will be executed, the method onDestroy() is executed when you press the back button or when you call the method finish() to force the activity to be destroyed , and then when you try to re launch your activity , the onCreate() will be executed.
refer this
The problem was with how I was setting the ImageView image. My original way to load an image from /res was:
image.setImageDrawable(getResources().getDrawable(R.drawable.myImage)); //WRONG!!!!
apparently if you recycled a bitmap, the above code will not reallocate memory for the bitmap, and your program will crash when it tries to draw that ImageView.
The correct way to load a bitmap that has been recycled (or at least, the way that solved my problem) is:
image.setImageDrawable(new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.myImage))); //correct!
It still doesn't answer my question as to why, after exiting the app, and onDestroy is called, that when I re-enter the app, it is looking for a recycled bitmap. In theory the app should be starting up from scratch.
It looks like anything that occurs in onResume() wont have to be placed in onCreate since onResume() is always run after onCreate()? I'm asking this because i have an algorithm that must be executed every time a user comes back to the main activity. If it is already in onResume() then do i have to have it in onCreate()?
No, you don't need it. onResume is always called after onCreate so if onCreate runs so will onResume.
You are correct. onResume is always called after onCreate.
Basically so. but you need to think what pause/resume and start/stop mean for your app. Also when you are in OnCreate, the activity is not yet created, where as in onStart is it. This may have impact in properties of the activity that you seek to use/change.