Android Programming
Android Programming
Lecture 11, 12
Contents
• Intents in Android
• Toast in android
• Introduction to Gradle
• Context
• Bundle
• Memory Leaks in Android
Intent in Android
• Android uses Intent for communicating between the components of
an Application and also from one application to another application.
• Intent are the objects which is used in android for passing the
information among Activities in an Application and from one app to
another also. Intent are used for communicating between the
Application components and it also provides the connectivity
between two apps.
Example
• For example: Intent facilitate you to redirect your activity to another
activity on occurrence of any event. By calling, startActivity() you can
perform this task.
Methods Description
Intent Types
Implicit Intent
Explicit Intent
Implicit Intent:
• In Implicit Intents we do need to specify the name of the component.
We just specify the Action which has to be performed and further this
action is handled by the component of another application.
• The basic example of implicit Intent is to open any web page
• Let’s take an example to understand Implicit Intents more clearly. We
have to open a website using intent in your application. See the code
snippet given below
• Unlike Explicit Intent you do not use any class name to pass through
Intent(). In this example we has just specified an action. Now when
we will run this code then Android will automatically start your web
browser and it will open home page of mentioned website.
Explicit Intent:
• Explicit Intents are used to connect the application internally.
• In Explicit we use the name of component which will be affected by
Intent. For Example: If we know class name then we can navigate the
app from One Activity to another activity using Intent. In the similar
way we can start a service to download a file in background process.
Explicit Intent:
• Explicit Intent work internally within an application to perform
navigation and data transfer. The below given code snippet will help
you understand the concept of Explicit Intents
• Here SecondActivity is the JAVA class name where the activity will
now be navigated.
Task: Intents in Android practice
• Create a project in Android Studio and named it “Intents”. Make an
activity, which would consists Java file; MainActivity.java and an xml
file for User interface which would be activity_main.xml
Implicit Intent
• Using implicit Intent, components can’t be specified. An action to be
performed is declared by implicit intent. Then android operating
system will filter out components that will respond to the action. For
Example,
• In the above example, no component is specified, instead, an action is
performed i.e. a webpage is going to be opened. As you type the
name of your desired webpage and click on the ‘CLICK’ button. Your
webpage is opened.
Step by Step Implementation
Creating an Android App to Open a Webpage Using Implicit Intent
Step 1: Create a New Project in Android Studio
• To create a new project and select empty activity in Android Studio
Step 2: Working with the XML Files
• Next, go to the activity_main.xml file, which represents the UI of the
project. Below is the code for the activity_main.xml file. Comments
are added inside the code to understand the code in more detail.
Code
• Refer to this URL to get code for both files xml and java
PRACTICE CODES
Java Code Explanation of important Terms
• onCreate(Bundle savedInstanceState): This is a lifecycle method in
Android activities. It is called when the activity is first created. Here,
we override this method to perform initialization tasks, such as setting
up the layout of the activity and initializing UI components.
• super.onCreate(savedInstanceState): This line calls the onCreate()
method of the superclass (AppCompatActivity). It's essential to call
this method as the first statement in the overridden onCreate()
method to ensure that the activity is properly initialized.
Java Code Explanation of important Terms
• setContentView(int layoutResID): This method sets the layout
resource file for the activity. It defines the user interface of the
activity using an XML layout file. In the provided code,
setContentView(R.layout.activity_main) sets the layout of the activity
to the XML layout file named activity_main.xml.
• findViewById(int id): This method is used to find a view (such as a
button or text field) that was defined in the XML layout file by its ID. It
returns the view object that corresponds to the specified ID. In the
code, findViewById(R.id.btn) finds the button with the ID btn.
Java Code Explanation of important Terms
• setOnClickListener(View.OnClickListener listener): This method sets an
OnClickListener for the view, which listens for clicks on the view. When
the view is clicked, the onClick() method of the OnClickListener is called.
In the code, we set an OnClickListener for the button to handle clicks.
• Intent: An Intent is a messaging object that is used to request an action
from another app component. It can be used to start an activity, deliver a
broadcast, or start a service. In the code, we use an Intent with the action
Intent.ACTION_VIEW to open a web browser to view the specified URL.
• Uri: A Uri (Uniform Resource Identifier) identifies a resource on the
internet or within an app. In the code, we use Uri.parse(url) to parse the
URL string and create a Uri object that represents the URL.
Find View by id
• Method: The interface contains a single method onClick(View v), which is called when the view is
clicked.
• Implementation: To use an OnClickListener, you typically create an instance of it and override the
onClick() method with the desired behavior. This is usually done inline with an anonymous inner class
or using a lambda expression.
• In this example:
Constants Description
Methods Description
public static Toast makeText(Context makes the toast message consist of text
context, CharSequence text, int duration) and time duration
• It is known that Intents are used in Android to pass to the data from
one activity to another. But there is one another way, that can be
used to pass the data from one activity to another in a better way and
less code space ie by using Bundles in Android.
Bundle in Android with Example
• Android Bundles are generally used for passing data from one activity
to another. Basically here concept of key-value pair is used where the
data that one wants to pass is the value of the map, which can be
later retrieved by using the key.
• Bundles are used with intent and values are sent and retrieved in the
same fashion, as it is done in the case of Intent. It depends on the
user what type of values the user wants to pass, but A Bundle can
hold various types of data, including primitive types, arrays, and
serializable objects.
• Creating a Bundle:
• Replace XXX with the appropriate data type like String, Int, Parcelable,
etc. For example:
• Getting data from a Bundle:
• Again, replace XXX with the appropriate data type. For example:
Memory Leaks in Android
• Memory leaks in Android occur when objects that are no longer
needed are not properly released from memory. This can lead to an
increase in memory usage over time, potentially causing the app to
slow down, become unresponsive, or even crash. Memory leaks are a
common issue in Android development, and they often happen due
to the following reasons:
Causes of Memory Leaks and Their
Solutions
• Memory leaks are a common issue in Android development, and they
often happen due to the following reasons:
• 1. Using Static Views: One should not use static views while
developing the application, as static views are never destroyed.
Memory Leaks Reasons
2. Using Static Context
• One should never use the Context as static, because that context will
be available through the life of the application, and will not be
restricted to the particular activity.
• 3. Using Code Abstraction Frequently
• Developers often take the advantage of the abstraction property
because it provides the code maintenance and flexibility in code, but
using abstraction might cost a bit to the developer, as while using
abstraction one needs to write more code, more code means more
time to execute the space and more RAM. So whenever one can avoid
the abstraction in the code, it is code as it can lead to fewer memory
leaks.
• 4. Unregistered Listeners
• When the developer uses any kind of listener in his application code,
then the developer should not forget to unregister the listener.
• 5. Unregistered Receivers
• Many times the developer needs to register the local broadcast
receiver in an activity. However, if the developer does not unregisters
the broadcast receiver there is a strong chance that our app can lead
to the memory leak problem as the receiver will hold a strong
reference to the activity. So even if the activity is of no use, it will
prevent the garbage collector to collect the activity for garbage
collection, which will ultimately cause the memory leak.
• 6. Inner class Reference
• An inner class is often used by the android developers within their
code. However, the nonstatic class will hold the implicit reference of
the parent class which can cause the memory leak problem. So there
can be two solutions that can be proposed to solve this problem:
• One can make the inner class static
• If one wants to pass the reference of the non-static inner class, then it
can be passed using weak reference.
• Use Memory Profiling Tools: Android Studio provides memory
profiling tools such as the Memory Profiler and Allocation Tracker,
which can help you identify memory leaks by analyzing the memory
usage of your app.