Android Activity
Android Activity is the fundamental building piece of an Android App. We have as of now seen this in the part enumerating Android Basics. Android Activity dependably has a User Interface. Android applications comprise of one or numerous Activities. We likewise saw that to tell Android, which action to open when the application is dispatched, we characterized Main Activity in Manifest.xml. An Android Activity has a Lifecycle amid which it performs a couple of things. The principal inquiry when perusing about Lifecycle of Activity Lifecycle is - Why is it obliged ? How about we comprehend it with an illustration. Accept you are playing an amusement on your telephone, you are at level 2 and abruptly somebody calls you. When you get a call, your diversions stop and you see the Caller Id Screen. When you continue your amusement after the call closes, it gets continued from the same point where you cleared out it. Presently accept you are a diversion engineer. For you, there ought to be an approach to spare the amusement state. Correct? In what capacity will this happen? To make designer's life less demanding, Android has something many refer to as Activity Lifecycle. Consider Lifecycle to be an accumulation of callback capacities getting called at whatever point something happens to your application (or Activity to be more precise).the above picture shows different capacities like 'oncreate()', "onstart()" which gets called at different purposes of an Activity Lifecycle. How about we take a gander at each of the routines/capacities in subtle element. Oncreate oncreate() is called when your Activity is getting made shockingly. It is called just once amid the whole Activity Lifecycle. One of the paramount things you should do is to situated the Activity Layout through setcontentview capacity. Likewise, you can utilize oncreate to instate your variables. In any Android application, at whatever point you make an Activity, the base technique which you have to override is oncreate. class Mainactivity broadens Activity @override ensured void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } end On the off chance that you recognize, Oncreate capacity is getting passed a variable of class Bundle. Group is regularly used to store the condition of your Activity. Take the illustration of screen pivot, amid which your Activity gets murdered and Oncreate is called once more. You can figure out whether the Activity was at that point there utilizing Bundle so you don't need to make the Activity once more. Why is this essential, envision you have a structure and client has effectively filled a percentage of the fields. All of a sudden the client pivots his screen. Utilizing Bundle, Android holds the estimations of these fields and re-populates the information after pivot naturally. The estimation of Bundle will dependably be invalid when Activity is getting made surprisingly. Onstart: onstart gets called just before the Activity gets to be obvious to the client. On the off chance that you recognize, onstart is called from two spots - after onrestart and Oncreate. onstart is constantly trailed by Onresume or Onstop. You can utilize onstart to reset Activity information, reinitialize variables and so forth. Onresume: onresume gets called when your Activity comes into the forefront, and it gets to be noticeable to the client. Right now, the Activity is on top of the Activity stack, and the client can begin collaborating with the Activity. onresume is ordinarily used to enlist Listeners, tie to Services and so forth. onresume is a decent place to invigorate your UI with any new changes which may have happened amid the period in which the Activity was not noticeable. Case in point, in the event that you are surveying a Service out of sight (like checking for new tweets), onresume is a decent place to redesign your screen with new comes about. Onpause: onpause is called when an alternate android movement goes ahead top of your Activity. Normally anything that takes your client far from your Activity will bring about onpause. In Onpause, we either discharge the assets, or recovery the application information, or stop foundation strings and so forth. It is constantly ensured that at whatever point your Activity is getting to be undetectable or part of the way imperceptible, onpause will be called. At the same time once onpause is called, Android maintains all authority to kill your Activity anytime. Subsequently you ought not be depending on accepting any further occasions. Onstop: onstop is called when your Activity is no more unmistakable to the client, it is like onpause yet here you won't see your android action completely. You can utilize this system too to store the condition of your application and close down time concentrated or CPU serious operations. This technique is ensured to be called as of API level 11. So what is the distinction in the middle of onpause and Onstop ? In the event that an Activity comes into the frontal area and fills the screen such that your current movement is not in the slightest degree obvious, your current android action will be called with both onpause and onstop . On the off chance that, in any case, an Activity that comes to forefront does not fill the screen and your current Activity is in part noticeable, your current Activity will be called with just onpause. Ordinarily at whatever point you see a dialog box which requires your consideration like battery low, organize association your current android action gets to be incompletely obvious and popup box goes ahead the top. This is the point where just onpause will be called. Onrestart: It is like oncreate, however onrestart gets called just after onstop. This is the system which you can use to know whether your application is beginning anew or getting restarted. In onrestart, you will get your application to spare the state and reinitialize all the variables. onstart gets called after this. Ondestroy: This is the technique which will be called when your Activity is getting slaughtered. This is the last call the Activity will get in its Lifecycle. At the point when the client press back catch on any Activity the closer view action gets crushed and control will come back to the past Activity.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
January 2015
Categories
|