Android Tutorial Theory
Android Tutorial Theory
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.
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.
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
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.
a. Explicit intent
b. Implicit 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.
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.
The code for starting activity to handle result may go like the following 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");
}
}
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.