Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Android Activity Lifecycle is controlled by 7 methods import android.database.sqlite.

SQLiteDatabase;
of android.app.Activity class. The android Activity is import android.database.sqlite.SQLiteOpenHelper;
the subclass of ContextThemeWrapper class. An public class DatabaseHelper extends
activity is the single screen in android. It is like SQLiteOpenHelper {
window or frame of Java. By the help of activity, you // Define table and columns here
can place all your UI components or widgets in a
// ...
single screen. The 7 lifecycle method of Activity
describes how activity will behave at different states. public DatabaseHelper(Context context) {
onCreate() Called when the activity will super(context, "TaskDatabase", null, 1);
start interacting with the user. At this point your }
activity is at the top of the activity stack, with user @Override
input going to it.,Always followed by onPause().
public void onCreate(SQLiteDatabase database)
onRestart()Called after your activity has been {
stopped, prior to it being started again.,Always
// Create the table
followed by onStart() onStart()Called when the
activity is becoming visible to the user.,Followed by // ...
onResume() if the activity comes to the foreground, or }
onStop() if it becomes hidden. onResume()Called @Override
when the activity will start interacting with the user. public void onUpgrade(SQLiteDatabase db, int
At this point your activity is at the top of the activity oldVersion, int newVersion) {
stack, with user input going to it.,Always followed by // Upgrade logic here
onPause(). onPause()Called when the system is about
// ...
to start resuming a previous activity. This is typically
used to commit unsaved changes to persistent data, }
stop animations and other things that may be }
consuming CPU, etc. Implementations of this method Adding Data: public long addTask(String
must be very quick because the next activity will not taskName) {
be resumed until this method returns.,Followed by ContentValues values = new ContentValues();
either onResume() if the activity returns back to the
values.put("task_name", taskName);
front, or onStop() if it becomes invisible to the user.
onStop()Called when the activity is no longer visible return database.insert("tasks", null, values);
to the user, because another activity has been resumed }
and is covering this one. This may happen either Updating Data: public int updateTask(long taskId,
because a new activity is being started, an existing one String newTaskName) {
is being brought in front of this one, or this one is ContentValues values = new ContentValues();
being destroyed.,Followed by either onRestart() if this values.put("task_name", newTaskName);
activity is coming back to interact with the user, or
return database.update("tasks", values, "_id = ?",
onDestroy() if this activity is going away.
new String[]{String.valueOf(taskId)});
onDestroy()The final call you receive before your
activity is destroyed. This can happen either because }
the activity is finishing (someone called finish() on it, Removing Data: public void deleteTask(long
or because the system is temporarily destroying this taskId) {
instance of the activity to save space. You can database.delete("tasks", "_id = ?", new String[]
distinguish between these two scenarios with the {String.valueOf(taskId)});
isFinishing() method. SQLite is a lightweight, }
embedded, relational database management system
Android Architecture Overview
(RDBMS) that is often used in mobile and desktop
applications for data storage. It is a self-contained,
serverless, and transactional database engine, making Linux Kernel: The foundation of the Android
it an excellent choice for applications that need a local operating system. It provides core services such as
database for data storage. Setting up the SQLite process management, hardware abstraction, and
Database: Create a Database Helper class to manage security. It acts as an intermediary between
your database: import android.content.Context; hardware and software. Hardware Abstraction
Layer (HAL): Situated above the kernel, the HAL executing Android applications. It was responsible
standardizes hardware access. This allows Android to for translating and executing the bytecode of
be compatible with various hardware configurations, Android apps, which were primarily written in Java.
making it adaptable across a wide range of devices. Key Roles of DVM: Execution of Android Apps:
Native Libraries: Essential components written in C DVM was the engine behind running Android apps.
and C++. These libraries offer core functionalities for It took the compiled Java code, transformed it into a
the operating system. Examples include OpenGL ES specialized bytecode format called "Dalvik
for 3D graphics and SQLite for database management. Executable" (DEX), and executed these DEX files.
Android Runtime (ART/Dalvik): Responsible for Efficient Bytecode Execution: DVM executed
executing application code. While Dalvik was used in bytecode efficiently using a register-based
older Android versions, ART is used in newer ones. architecture. This design choice was made to
These runtimes compile and execute Java/Kotlin code conserve system resources, especially on mobile
into machine code for enhanced performance. devices with limited processing power and memory.
Application Framework: The application framework Just-In-Time (JIT) Compilation: DVM employed a
provides high-level services and APIs for app Just-In-Time compilation approach. Instead of
development. It includes components like Activity translating all bytecode to native machine code
Manager, Content Providers, Resource Manager, and upfront, DVM translated it as needed during
Notification Manager. These components help manage runtime. This dynamic compilation optimized
application lifecycle, data access, resources, and performance by converting code into native
notifications. Libraries: Android offers various machine instructions at runtime, improving
libraries for different purposes. Notable libraries execution speed. Optimizations for Mobile
include Android Support Libraries (for backward Devices: DVM was tailored for mobile devices,
compatibility), Android Jetpack (for modern app focusing on minimizing memory consumption and
development), and Google Play Services (for maximizing battery life. It implemented techniques
accessing Google-related features). System like sharing identical strings across applications to
Applications: These are pre-installed apps that come reduce memory duplication In essence, the DVM
with the Android operating system, such as the phone was the core component responsible for running
dialer, contacts, and browser. They form an integral Android apps efficiently on early Android devices.
part of the Android experience and can be extended or However, it's important to note that with the
replaced by third-party apps User Applications: introduction of Android 5.0 and later versions, the
Installed by users from sources like the Google Play Dalvik Virtual Machine was largely replaced by the
Store. These apps use Android's APIs and libraries to Android Runtime (ART), which brought significant
provide various functionalities, from productivity tools performance improvements through Ahead-of-Time
to entertainment. User Interface (UI): The UI layer (AOT) compilation and other enhancements. A
includes elements like the home screen, status bar, fragment in Android is a modular and self-
system bars, and user-installed applications. Android contained user interface component that can be
offers a flexible UI framework that allows developers utilized to create flexible and reusable parts of an
to create diverse user interfaces. Application application's user interface. It is akin to a mini-
Sandbox: Each Android app runs in its own isolated activity within an activity and is used to divide the
environment, known as an application sandbox. This user interface and functionality into smaller,
ensures app security and prevents interference with manageable pieces. Fragments play a crucial role in
other apps. Apps can only access resources and data Android app development, particularly when
with appropriate permissions. In summary, Android's dealing with various screen sizes, orientations, and
architecture is designed as a layered system, with each dynamic UIs. Creating a fragment involves several
layer serving specific functions and responsibilities. steps. First, you define a Java class that extends the
This modular approach allows for flexibility, security, Fragment class. Optionally, you can create an XML
and the development of a wide range of applications layout file for the fragment to define its user
on the Android platform. The Dalvik Virtual interface. You then implement the fragment's logic,
Machine (DVM) played a pivotal role in the early including lifecycle methods like onCreateView for
versions of the Android operating system, prior to initializing the UI. Fragments can be added to an
Android 5.0 (Lollipop). Here's a medium-sized activity's layout XML using the <fragment> tag or
explanation of its role: The Dalvik Virtual Machine programmatically via a FragmentTransaction in the
(DVM) served as the runtime environment for activity's Java code.
Communication between fragments and their host import
activity is vital. Fragments can communicate with androidx.appcompat.app.AppCompatActivity;
their host activity using interfaces or other methods.
Additionally, fragments can interact with one another public class RegistrationActivity extends
through the host activity, enabling dynamic user AppCompatActivity {
interactions within the app. The fragment lifecycle
includes methods like onCreate, onCreateView,
onPause, and onResume. Properly managing the private EditText nameEditText, emailEditText,
fragment's state is essential to ensure the correct phoneEditText, addressEditText;
behavior of your app. Fragments are particularly private CheckBox agreeCheckBox;
valuable for creating responsive user interfaces. For private RadioGroup genderRadioGroup;
example, on larger screens like tablets, you can private Button submitButton;
display multiple fragments side by side within a single
private TextView resultTextView;
activity. On smaller screens, such as phones, you can
swap fragments within the same activity to optimize
the user experience. In summary, fragments are @Override
versatile and powerful components in Android app protected void onCreate(Bundle
development, allowing developers to create modular, savedInstanceState) {
reusable, and responsive user interfaces. By following super.onCreate(savedInstanceState);
the steps mentioned above, you can effectively setContentView(R.layout.activity_registration)
incorporate fragments into your Android applications ;
to enhance user experiences and simplify UI
management.
// Initialize UI components
In Android, an "Intent" is a crucial mechanism for
inter-component communication. It serves as a means nameEditText =
to request actions from different parts of an findViewById(R.id.nameEditText);
application or between separate Android applications. emailEditText =
Two primary types of intents exist: explicit and findViewById(R.id.emailEditText);
implicit. Explicit Intents are employed to pinpoint a phoneEditText =
specific component, typically an activity, within the findViewById(R.id.phoneEditText);
same application. They explicitly mention the target addressEditText =
component by its class or package name, ensuring the findViewById(R.id.addressEditText);
Android system knows precisely which component to
agreeCheckBox =
launch. This type is ideal for intra-app operations. On
findViewById(R.id.agreeCheckBox);
the other hand, Implicit Intents do not specify a
particular component but describe an action and, genderRadioGroup =
optionally, the data to work on. Android then searches findViewById(R.id.genderRadioGroup);
for registered components that can handle the task, submitButton =
allowing different apps to interact. Implicit intents findViewById(R.id.submitButton);
facilitate extensible and loosely coupled resultTextView =
communication between components and apps, findViewById(R.id.resultTextView);
fostering flexibility and interoperability. // Set OnClickListener for the submit button
import android.os.Bundle; submitButton.setOnClickListener(new
import android.view.View; View.OnClickListener() {
import android.widget.Button; @Override
import android.widget.CheckBox; public void onClick(View v) {
import android.widget.EditText; // Retrieve user input
import android.widget.RadioButton; String name =
import android.widget.RadioGroup; nameEditText.getText().toString();
import android.widget.TextView; String email =
emailEditText.getText().toString();
String phone =
phoneEditText.getText().toString();
String address =
addressEditText.getText().toString();
boolean agreed =
agreeCheckBox.isChecked();
RadioButton selectedGenderRadioButton =
findViewById(genderRadioGroup.getCheckedRadioB
uttonId());
String gender =
selectedGenderRadioButton.getText().toString();

// Display the registration details


String registrationDetails = "Name: " +
name + "\nEmail: " + email + "\nPhone: " + phone
+ "\nAddress: " + address + "\nAgreed
to Terms: " + agreed + "\nGender: " + gender;
resultTextView.setText(registrationDetails);
}
});
}
}

You might also like