0% found this document useful (0 votes)
7 views

Android Tutorial Theory

The document explains key terms in Android programming, focusing on Activities and Intents. An Activity represents a single screen with a user interface, and it follows a lifecycle managed by callback methods. Intents are used to start activities or services, with explicit intents specifying the target component and implicit intents describing the action to be performed.

Uploaded by

mauahezekia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Android Tutorial Theory

The document explains key terms in Android programming, focusing on Activities and Intents. An Activity represents a single screen with a user interface, and it follows a lifecycle managed by callback methods. Intents are used to start activities or services, with explicit intents specifying the target component and implicit intents describing the action to be performed.

Uploaded by

mauahezekia
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Terms Used In Android Programming

1. Activity: This is basically a page in an application. In java programming there is what is called the
main activity. This means the main activity is the entry point of an application. In your history of
programming you’ve always met the word main e.g in C++ we have the main function written
this way
Int main () meaning it is the entry point of the application then using intents will open other
activities.

In Android programming, an activity is a fundamental component that represents a single screen


with a user interface, allowing users to interact with the app. It is a Java class that manages the
layout and components of the screen, defined using an XML file. Activities go through a lifecycle
with various states such as onCreate(), onStart(), onResume(), onPause(), onStop(), and
onDestroy(), which are managed by callback methods. These lifecycle methods help developers
control the state and behavior of the activity effectively.

Introduction to Activities in Android




Activity class is one of the very important parts of the Android Component. Any app, don’t
matter how small it is (in terms of code and scalability), has at least one Activity class. Unlike
most programming languages, in which the main() method is the entry point for that program or
application to start its execution, the android operating system initiates the code in an Activity
instance by invoking specific callback methods that correspond to specific stages of
its Lifecycle. So it can be said that An activity is the entry point for interacting with the user.
Every activity contains the layout, which has a user interface to interact with the user. As we
know that every activity contains a layout associated with it, so it can be said that the activity
class is the gateway, through which a user can interact programmatically with the UI. Layout for
a particular activity is set with the help of setContentView(). setContentView() is a function that
takes View as a parameter. The view parameter basically contains the layout file for that
activity.

The Following Code Indicates that activity_main is the Layout File


of MainActivity

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}
}

2. Intent

In android programming, an intent is used to start an activity, start a service, deliver a broadcast
or perform other operations.

There are two types of intent:

a. Explicit intent
b. Implicit intent

Explicit Intent and how to use it


Explicit intents specify the exact component to be launched, such as a specific activity or service.
You create an explicit intent by providing the class name of the target component.

Starting an Activity Using Explicit intent


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

Starting a Service Using Explicit intent


Intent intent = new Intent(this, MyService.class);
startService(intent);

Implicit Intent and how to use it


Implicit intents do not specify the target component but describe the action to be performed,
allowing the system to determine which component should handle the intent.

Viewing a webpage using implicit intent

Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.javatpoint.com"));


startActivity(intent);

Passing data using implicit intent


Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("key", "value");
startActivity(intent);

In the code above the second activity is the target activity where the data is passed to. After
passing the data then one can retrieve the data in the target activity using the following code.

String value = getIntent().getStringExtra("key");

The statement above means getting some data from an activity that passed the data. After
getting the data, there is need to receive the data and handle it in some manner that is
understandable by the user.

To handle Result from an activity we do the following

- We create another activity to display the result.


- The code for this could go like the following.

startActivityForResult(); and handle the result in onActivityResult();

Starting an activity for result

The code for starting activity to handle result may go like the following code.

Intent intent = new Intent(this, SecondActivity.class);


startActivityForResult(intent, REQUEST_CODE);

Target Activity

This is the code that goes in the target activity. The target activity should be known to be the
activity where the data is retrieved from.
Intent resultIntent = new Intent();
resultIntent.putExtra("result", "result value");
setResult(RESULT_OK, resultIntent);
finish();

Original Activity

In the original activity where the data was pushed from, this is the code that pushes the data to
the target activity.
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
String result = data.getStringExtra("result");
}
}

Using Intents to Start a Service

When starting a service, always use an explicit intent to ensure that only the intended service is
started. Avoid using implicit intents for services to prevent security risks.

Conclusion
Intents are a fundamental part of Android programming, enabling communication between
different components and applications. By understanding how to use explicit and implicit
intents, you can effectively manage the flow of your application and interact with other apps on
the device.

You might also like