Note: a few of the content of this post is taken from this StackOverflow answer.
Activity class indirectly extends Context. Hence, if your refer to
Context is also useful to do many other things. Check it out yourself in your IDE.
Now, there are various callback methods which you can use at these various states of your app:
In the above shown onCreate() method is what you typically see when you create a brand new empty Activity. Now let’s disect what actually is happening in there!
First,
To put the payload data,
To get the payload data in the next Activity,
Now you know what Bundle is, we’ll come to what
I would like to link a really good video which explains what
Similar to onCreate() method, you can use other callbacks like onStop(), onResume() etc. to do stuff like, for example, stopping CPU intensive process of your app in onPause() or onStop() so that the battery is used efficiently.
In the next post, we will try to focus on UI aspect of the Activity (like Layout, Adapter etc.)
(to be continued…)
Context
Context is a very important concept and you will be using it throughout your application. It is an abstract Java class defined aspublic abstract class Context extends Object by the Android system.What is Context?
- Context represents a handle to get environment data .
- Context class itself is declared as abstract, whose implementation is provided by the android OS.
- Context is like remote of a TV & channels in the television are resources, services, etc.
How to get the Context?
Ways to get context :this(inside a Service, Activity or any other class that directly or indirectly extendsContext)getApplicationContext()getContext()getBaseContext()
What can you do with it ?
- Loading resource.
this.getResources() inside an Activity.- Launching a new activity.
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
startActivity(intent);
this in your Activity or use YourActivity.this,
then it refers to Context instance as well. The constructor of the
Intent class expects the Context as the first parameter (we are getting
that instance of Context using YourActivity.this) and the Class object (instance of java.lang.Class) as the second parameter (we are getting that instance of Class using SecondActivity.class ).- Creating views.
TextView tv = new TextView(this);- Obtaining system service.
this.getSystemService(<Service name>);Context is also useful to do many other things. Check it out yourself in your IDE.

Now that you know what Intent, Context and Activity are, you will be able to build an app with multiple Activities!
Let’s dig deeper into how the Activity actually functions!Lifecycle of an Activity
An Activity has following states:-
Created
-
Started
-
Resumed
-
Paused
-
Stopped
-
Destroyed
Now, there are various callback methods which you can use at these various states of your app:
-
onCreate()
-
onStart()
-
onResume()
-
onPause()
-
onStop()
-
onDestroy()
onCreate()
method. It is a mandatory method which is needed to be overrided
because, it is a method where you will choose which XML file you want
the Activity to use for UI (using setContentView() method).In the above shown onCreate() method is what you typically see when you create a brand new empty Activity. Now let’s disect what actually is happening in there!
First,
super.onCreate(savedInstanceState) is called for letting the Android system to do it’s thing. Then we call setContentView()
and give it the XML file that we want the Activity to use for UI. After
that, we may use some other logic which depends on our needs. Now,
notice that we are passing Bundle savedInstanceState as a parameter to onCreate() method. A Bundle is a class that holds key-value pairs (like Map
class in classic Java) and is used to pass data between Activities.
Earlier, I told you how Intent is used to navigate from one Activity to
another and you can put some payload data for the Intent to carry to the
next Activity. The Intent actually takes your payload data and puts it
into a Bundle.To put the payload data,
To get the payload data in the next Activity,
Now you know what Bundle is, we’ll come to what
Bundle savedInstanceState is.I would like to link a really good video which explains what
Bundle savedInstanceState is for: Coding in Flow, How to Restore Variables When Rotating the DeviceSimilar to onCreate() method, you can use other callbacks like onStop(), onResume() etc. to do stuff like, for example, stopping CPU intensive process of your app in onPause() or onStop() so that the battery is used efficiently.
In the next post, we will try to focus on UI aspect of the Activity (like Layout, Adapter etc.)
(to be continued…)
Comments
Post a Comment