Notes - Android

What is a Context?

Context is used to provide information about application environment, and is used to perform application level tasks. Such as; Launching Activity, Broadcasting and Receiving intents and etc.

Context are used when our code needs to access some System Service or User Interface.

There are totally 2 types of Context:

  1. Activity Context
  2. Application Context.

Activity class indirectly inherits from the Context class, so that means every activity itself is a context.

In order to get the Application level context getApplicationContext() is used. It is from the activity class.

What is an Adapter?

Adapter is used to populate the AdapterViews such as ListView, Spinner etc. Dynamically at a run time.

image.png

Adapter takes the data in the Data Source / Model. Then it generates the list items based on the layout specified in the ListView. Sets the data from Model in to those views, and then populates the AdapterView.

Later, when changes occur in the Model / Data Set. Developer should notify the Adapter about the change, and it should update the Adapter automatically. Developer can notify about the changes using notifyDatasetChanged

What things one can place inside Android Manifest File.

  1. Package Name
  2. Application Name
  3. ID of application icon
  4. Theme used by the Application
  5. All the activities and the Intent - Filters for the Intents they would like to receive
  6. Permission needed by the application
  7. Content Providers in the application
  8. Services in the application
  9. Broadcast Receiver in the application, and the intent filters for the messages they would like to receive.

What is Intent? Difference Between Implicit Intent and Explicit Intent.

Intent is an abstract description about an operation to be performed. Intent is a message object which is used to communicate between the different component of the application such as Activity, Broadcasting and Services.

Typically, intent is used when some event occurs and it requires some action to be performed.

Implicit Intent

Here, we do not define the Component, rather we define the action to be performed. Based on the action, android device itself will decide which component to use. If there is only one component for that action, android will simply execute that action in that component. If there are more than one then user is given choice, based on the user's selection Action will be executed in that component.

image.png

Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("https://tushar24.pythonanywhere.com/");

startActivity(i);

Explicit Intent

Here, we can define the component. Once target component is defined, android can simply execute that component. This type of intent are largely used for Navigating between the activity.

Intent i = new Intent(MainActivity.this, SecondActivity.class);
startActivity(i);

Second parameter here is the targeted component, we can also share data between the activities with this type of intent. Such as;

Intent i = new Intent(MainActivity.this, SecondActivity.class);
i.putExtra("name", "tushar");
i.putExtra("age", 21);
startActivity(i);

In the receiving activity, one should access the data this way.

String name = getIntent().getStringExtra("name", "null");