Skip to main content

My Android notes - 2

Note: a few of the content of this post is taken from this StackOverflow answer.

Context

Context is a very important concept and you will be using it throughout your application. It is an abstract Java class defined as public 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.
Just keep in mind that Context contains the current state of the application/object. It lets newly-created objects understand what has been going on.

How to get the Context?

Ways to get context :
  • this (inside a Service, Activity or any other class that directly or indirectly extends Context)
  • 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);
Activity class indirectly extends Context. Hence, if your refer to 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:
  1. Created
  2. Started
  3. Resumed
  4. Paused
  5. Stopped
  6. Destroyed
State 1, 2 and 3 happen sequentially when the Activity initializes for the first time. State 4 is happens when another Activity of your app comes into display. State 5 is called when, for example, you press the home button and the app is minimized. State 6 is called when you quit the app.
Now, there are various callback methods which you can use at these various states of your app:
  1. onCreate()
  2. onStart()
  3. onResume()
  4. onPause()
  5. onStop()
  6. onDestroy()
Although you might have never heard of any of the callback methods from 2 to 6, you have certainly seen 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 Device

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…)

Comments

Popular posts from this blog

My-Cloud-IDE - a SaaS built using Docker

I always wanted to know about how services like Codenvy  and Heroku  internally worked. The only way to understand that was to build a similar project. The main component of this project is Docker. Docker  Docker is a software that performs OS-level virtualisation and is developed for Linux. More information about it can be found here . About My-Cloud-IDE: My-Cloud-IDE is a proof-of-concept for a Software as a Service (SaaS). It   can be used to perform software development on cloud without worrying about resolving software dependencies, software installations etc. The user gets a fully functional IDE in his/her browser after registration. Each user has his/her own isolated software environment because My-Cloud-IDE uses Docker to perform OS-level virtualisation in the backend. Technical Details I divided the project into two modules. One module was for 'management' purpose. This includes user interfaces, user management and preparing some files that a...

How did I setup a Rasberry Pi 3 without any peripherals

I am an absolute beginner in this Raspberry Pi (I'll be using "RPi" for short) world and this post shows how I started up the RPi without using external keyboard or mouse (for RPi) and still managed to get a graphical desktop environment of Raspbian on my Debian workstation (and controlled my RPi remotely). Step 1: I downloaded 'Raspbian Stretch with Desktop' ( here ). I got a ZIP file. I unzipped it and ended up with a .img which was roughly 4.6 GB. Step 2: Now, to create a bootable media (a memory card perhaps) by copying the .img file onto it, I used dd command. sudo dd if=<path to img> of=/dev/<sdb,sdc..> Be particularly careful with using dd command. You can mess up a lot of things. Using dd command on your memory card will wipe out all contents in it. Using dd command on your hard disk, umm.. not a good idea. To know the disk path of your memory card, use sudo fdisk -l  (el in small caps) . Your hard disk will probably be /dev/sda. You...

Privacy Concerns in Android

Introduction       Android has a robust Access control system. If an Android app needs to use resources or information outside of its own scope, the app has to request the appropriate permission . It needs to get permissions before accessing critical resources like Camera, Location, Contacts etc. On Android 6.0 (Marshmallow) or higher, the app needs to request these permissions at runtime by showing a dialog box to the user. Also, the user will have an option to revoke these permissions for that app at any time in future.       But, in this article, I would like to point out a few resources that an app can access without any permission s and this might raise a serious privacy concern for the user. Privacy Concerns An app can fetch a list of all other apps installed on your phone. An app doesn't need to request any permissions from you to get a list of all the apps installed on your phone. For example, a Netflix app can get to know wh...