0% found this document useful (0 votes)
11 views5 pages

Unit2 Compressed

The document discusses the Android activity lifecycle and its methods. It describes the four states an activity can be in - does not exist, foreground, background, and pause. It also lists the seven lifecycle methods an Android activity uses: onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), and onDestroy(). Each method is called when the activity transitions between the different states.

Uploaded by

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

Unit2 Compressed

The document discusses the Android activity lifecycle and its methods. It describes the four states an activity can be in - does not exist, foreground, background, and pause. It also lists the seven lifecycle methods an Android activity uses: onCreate(), onStart(), onResume(), onPause(), onStop(), onRestart(), and onDestroy(). Each method is called when the activity transitions between the different states.

Uploaded by

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

Android Activity

Android Activity Lifecycle


Lifecycle methods
Activity doesn’t exist
• An activity is the single screen in android. It is like
window or frame of Java. Ondestroy() Oncreate()
• Java File(activity)

UNIT-2- ANDROID • XML File(Layout)


• Activities having four states
Onstop()

Onpause()
Onstart()

Onresume()
• Activity doesn’t exist
UI DESIGN • Foreground activity
• Background activity Onresume()
Foreground Activity Pause(onpause(),
onresume())

• Pause Onpause()
Onstart()
• Android Activity Lifecycle is controlled by 7 methods
of android.app.Activity class. The android Activity is Onrestart() Onstop()
the subclass of MainActivity class. Background Activity

Android Activity
File: MainActivity.java File: MainActivity.java File: MainActivity.java
Lifecycle methods
• package example.javatpoint.com.activitylifecycle; • @Override • @Override
• import android.app.Activity; • protected void onStart() { • protected void onPause() {
• import android.os.Bundle; • super.onStart(); • super.onPause();
• import android.util.Log; • Log.d("lifecycle","onStart invoked"); • Log.d("lifecycle","onPause invoked");
• public class MainActivity extends Activity {
• } • }
• @Override
• @Override • @Override
• protected void onCreate(Bundle savedInstanceStat
e) { • protected void onResume() { • protected void onStop() {
• super.onCreate(savedInstanceState); • super.onResume(); • super.onStop();
• setContentView(R.layout.activity_main); • Log.d("lifecycle","onResume invoked"); • Log.d("lifecycle","onStop invoked");
• Log.d("lifecycle","onCreate invoked"); • } • }
• }

File: MainActivity.java Android Nougat Android V7 features Android V7 libraries


• @Override • Android Nougat (codenamed Android N during • skin tone modifier support • v7 appcompat library
• protected void onRestart() { development) is the seventh major version and 14th • Ability to display color calibration • v7 cardview library
• super.onRestart(); original version of the Android operating system. • Ability to screen zoom • v7 gridlayout library
• Log.d("lifecycle","onRestart invoked"); • The LG V20 was the first smartphone released with • Daydream VR platform • v7 mediarouter library
• } Nougat.
• Improvements to file browser • v7 palette library
• @Override • Nougat introduces notable changes to the operating
• Multi-window support • v7 recyclerview library
• protected void onDestroy() { system and its development platform, including the
• super.onDestroy(); ability to display multiple apps on-screen at once in a • Keyboard image insertion • v7 Preference Support Library
• Log.d("lifecycle","onDestroy invoked"); split-screen view, support for inline replies to • Battery usage alerts
• } notifications, and an expanded Doze power-saving
•} mode that restricts device functionality once the
screen has been off for a period of time.

v7 appcompat library v7 appcompat library Android CardView v7 cardview library


• ActionBar - Provides an action bar user interface • AppCompatDialog - Adds a dialog class that can be • Android CardView UI component shows information • This library adds support for the CardView widget,
pattern. used as a base class for AppCompat themed dialogs. inside cards. This component is generally used to which lets you show information inside cards that
• android.support.v7.app.ActionBar • ShareActionProvider - Adds support for a standardized show contact information. This component is available have a consistent look on any app.
• AppCompatActivity - Adds an application activity class sharing action (such as email or posting to social in another support library so we have to add its • These cards are useful for material design
that can be used as a base class for activities. applications) that can be included in an action bar. dependency too. implementations, and are used in layouts for TV apps.
• android.content.Context • android.view.View
• android.content.ContextWrapper • android.view.ViewGroup
• android.view.ContextThemeWrapper • android.widget.FrameLayout
• android.app.Activity • android.support.v7.widget.CardView
• android.support.v4.app.FragmentActivity
• android.support.v7.app.AppCompatActivity
v7 gridlayout library v7 mediarouter library v7 palette library
• This library adds support for the GridLayout class, • It provides MediaRouter, MediaRouteProvider, and • The v7 palette support library includes • RecyclerView is a efficient way to create a list of items.
which allows you to arrange user interface elements related media classes that support Google Cast. the Palette class, which extracting color variations It is upgraded version of listView with more features
using a grid of rectangular cells • The APIs in the v7 mediarouter library provide a from an image. in it. It is included in Lollipop(API level 22 ) in the
• java.lang.Object means of controlling the routing of media channels • For example, a music app could use a Palette object to support-v7 library.
• android.view.View and streams from the current device to external screens, extract the major colors from an album cover, and use • RecyclerView is used to handle thousands of items in
• android.view.ViewGroup speakers, and other destination devices. those colors to build a color-coordinated song title the list and for that it create a limited number of view
• android.support.v7.media card. so to manage memory efficiently .
• android.widget.GridLayout

v7 Preference Support
v7 recyclerview library API Level: 21 Intent
Library
• The recyclerview library adds the RecyclerView class. • The preference package provides APIs to support • Android 5.0 (LOLLIPOP) offers new features for users • An Intent is a messaging object you can use to request
• This class provides support for the adding preference objects, such and app developers. an action from another app component.
RecyclerView widget, a view for efficiently displaying as CheckBoxPreference and ListPreference, for users to • Support for 64-bit CPUs
large data sets by providing a limited window of data modify UI settings. • Support for print previews
items. • The v7 Preference library adds support for interfaces, • Material design, bringing a restyled user interface.
such as
• Project Volta, for battery life improvements.
• Preference.OnPreferenc eChangeListener
• Smart lock feature.
• Preference.OnPreferenceClick Listener
• Updated emoji.
• and classes, such as
• A flashlight-style application is included.
• CheckBoxPreference
• ListPreference

Intent Intent Intent Intent


• Intents facilitate communication between components in • Starting a service • Delivering a broadcast
several ways.
• A Service is a component that performs operations in • A broadcast is a message that any app can receive.
• Intents used for
the background without a user interface. • The system delivers various broadcasts for system
• Starting an activity
• An Activity represents a single screen in an app. • With Android 5.0 (API level 21) and later, you can events, such as when the system boots up or the
• You can start a new instance of an Activity by passing start a service with JobScheduler. device starts charging.
an Intent to startActivity(). • For versions earlier than Android 5.0 (API level 21), • You can deliver a broadcast to other apps by passing
• The Intent describes the activity to start and carries any you can start a service by using methods of the an Intent to sendBroadcast() (or) sendOrderedBroadc
necessary data. Service class. ast().
• If you want to receive a result from the activity when it • Example
finishes, call startActivityForResult().
• Your activity receives the result as a separate Intent object • Open email, web browser and calling
in your activity's onActivityResult() callback.

Intent types Intent types Intent Intent


• Explicit Intent • [1] Activity A creates an Intent with an action
• Communicates between two activities inside the same description and passes it to startActivity().
application. • [2] The Android System searches all apps for an intent
• Example filter that matches the intent.
• Start a service to download a file in the background. • When a match is found,
• Implicit intents • [3] the system starts the matching activity (Activity B)
• Communicates between two activities of different by invoking its onCreate() method and passing it
applications. the Intent.
Intent Objects Intent Objects
• An Intent object is a bundle of information which is • The action largely determines how the rest of the
used by the component that receives the intent as well intent object is structured . The Intent class defines a
as information used by the Android system. number of action constants corresponding to different
• An Intent object can contain the following intents.
components. • Action - The general action to be performed, such
• Action as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.
• This is mandatory part of the Intent object and is a • Data
string naming the action to be performed — or, in the • Adds a data specification to an intent filter. The
case of broadcast intents, the action that took place specification can be just a data type (the mime Type
and is being reported. attribute), just a URI, or both a data type and a URI.

Intent Objects Intent Objects Intent Objects


• Category • A string containing additional information about the • Extras
• The category is an optional part of Intent object and kind of component that should handle the intent. • This will be in key-value pairs for additional
it's a string containing additional information about • Any number of category descriptions can be placed in information that should be delivered to the
the kind of component that should handle the intent. an intent, but most intents do not require a category. component handling the intent. The extras can be set
• Here are some common categories: and read using the putExtras() and getExtras()
• The addCategory() method places a category in an
Intent object, removeCategory() deletes a category • CATEGORY_BROWSABLE methods respectively.
previously added, and getCategories() gets the set of • The target activity allows itself to be started by a
all categories currently in the object. web browser to display data referenced by a link,
such as an image or an e-mail message.
• CATEGORY_LAUNCHER
• The activity is the initial activity of a task and is
listed in the system's application launcher.

Intent Filter inter filter Intent adding category


• https://developer.android.com/guide/components/in • You will use <intent-filter> element in the manifest file to • A string containing additional information about the
tents-filters list down actions, categories and data types associated kind of component that should handle the intent.
with any activity, service, or broadcast receiver. • Any number of category descriptions can be placed in
• Intent filter tell the OS how to communicate with the • contained in:
different components(activities, services, broadcast an intent, but most intents do not require a category.
receives). • <activity> • Here are some common categories:
• If we want declare intent filter we need add information • <activity-alias>
into manifest file.
• CATEGORY_BROWSABLE
• <service>
• syntax: • The target activity allows itself to be started by a
• <receiver>
• <intent-filter android:icon="drawable resource" web browser to display data referenced by a link,
• must contain: such as an image or an e-mail message.
• android:label="string resource"
• <action>
• android:priority="integer" > • CATEGORY_LAUNCHER
• ... • can contain:
• The activity is the initial activity of a task and is
• </intent-filter> • <category>
listed in the system's application launcher.
• <data>

linking activities linking activities linking activities linking activities


• An Android application can contain zero or more • 1. Create new project with name “LinkingActivity” • Now see acitivty_main.xml file code • Now you need second UI for second activity : Right
activities. • 2. Open res ➪ layout ➪ activity_main.xml , add button Click on layout folder ➪ New ➪ Android XML file
• When your application has more than one activity, you simple drag drop or follow this step’s how to add (pop up box open see figure)
may need to navigate from one activity to another. button in Android Button example , after adding new
• In Android, you navigate between activities through button change its button name to “Go To Next “ • 4. Now click add text in
what is known as an intent. second_activity.xml
➪ “This is second Activity”
user interface design user interface design
linking activities linking activities
components components
• Code of second_activity.xml • Basic Layouts • A typical user interface of an android application
• • Input Controls consists of action bar and the application content area.
• Toast, Dialog and Snackbar • Main Action Bar
• Navigation, ActionBar and Menus
• View Control
• Design Support Library
• Image View • Content Area
• Digital Clock • Split Action Bar
• Analog Clock
• Text Clock
• SeekBar Example
• Calendar View
• Drop Down List

Views and View Groups Views and View Groups Basic View
• View Class are the basic building block for user • View Group Class • Button: Button is pushed or pressed or clicked.
interface components. • The View-Group is the base class for layouts, which • Every view should have a unique identifier which
• A View occupies a 2-dimensional area (say: rectangle) holds other Views and define their properties. identifies the element in the project. Make sure, two
on the screen, which is responsible for framing and • Actually an application comprises combination and elements should not share their unique ID. We
handling different type of events. nesting of Views-Group and Views Classes. implement certain methods and interfaces which
• Views are used to create input and output fields in the listens to user input and act accordingly.
an Android App. • TextView: This view is basically meant for displaying
• It can be input text field, radio field, image field etc. the text of application. For example the label of
• They are same as, input text field, image tag to show application is displayed here.
images, radio field in HTML.

Basic View Picker View Picker View Adapter View


• EditText: This is a modification of Text View. It is • Android provides controls for the user to pick a time • We recommend that you use DialogFragment to host • Adapter and Adapter View are so popular, that every
editable. User can give input dynamically. or pick a date as ready-to-use dialogs. each time or date picker. time you see any app with a List of items or Grid of
• Check Box: This widget is a two-state button. It can be • Each picker provides controls for selecting each part • The DialogFragment manages the dialog lifecycle for items, you can say for sure that it is using Adapter and
either checked or unchecked. of the time (hour, minute, AM/PM) or date (month, you and allows you to display the pickers in different Adapter View.
• Radio Button: This is also a two-state button. It can layout configurations, such as in a basic dialog on
day, year).
be checked or unchecked. It can be checked handsets or as an embedded part of the layout on
dynamically. • Using these pickers helps ensure that your users can large screens.
pick a time or date that is valid, formatted correctly, • Although DialogFragment was first added to the
• Radio Group: This houses radio buttons together.
Checking any one of the radio buttons in the group and adjusted to the user's locale. platform in Android 3.0 (API level 11), if your app
unchecks rest of the buttons. supports versions of Android older than 3.0—even as
• Image Button: This a button with image. User can low as Android 1.6—you can use
press or click this button. Image on the button is the DialogFragment class that's available in
referenced from the src folder of our project. the supportlibrary for backward compatibility.

Adapter View Different Layouts Different Layouts Different Layouts


• Types of layout • Linear Layout • AbsoluteLayout
• Linear Layout • Linear layout is further divided into horizontal and • The AbsoluteLayout enables you to specify the exact
• Absolute Layout vertical layout. It means it can arrange views in a location of its children. It can be declared like this.
• Table Layout single column or in a single row. Here is the code of
linear layout(vertical) that includes a text view.
• Frame Layout
• Relative Layout
Different Layouts Different Layouts Different Layouts App Widgets
• TableLayout • RelativeLayout • FrameLayout • App widgets can be thought of as a small window or
• The TableLayout groups views into rows and • The RelativeLayout enables you to specify how child • The FrameLayout is a placeholder on screen that controller for an Android app that can be embedded in
columns. It can be declared like this. views are positioned relative to each other. It can be you can use to display a single view. It can be another application (like the homescreen).
declared like this. declared like this. • They can be very useful, allowing users to view or
control an app without actually launching it.

Lollipop Material Lollipop Material Lollipop Material


RecyclerView
design design design
• Android 5.0 brings Material design to Android and •A new system-managed processing thread • ART also dynamically moves memory to optimize • The RecyclerView class supports the display of a
gives you an expanded UI toolkit for integrating the called RenderThread keeps animations smooth even performance for foreground uses. collection of data.
new design patterns easily in your apps. when there are delays in the main UI thread. • Notifications • It is a modernized version of the ListView and
• New 3D views let you set a z-level to raise elements off • ART runtime, built from the ground up to support a the GridView classes provided by the Android
mix of ahead-of-time (AOT), just-in-time (JIT), and • Notifications in Android 5.0 are more visible,
of the view hierarchy and cast realtime shadows, even accessible, and configurable. framework.
interpreted code.
as they move. • Varying notification details may appear on the lock • Recycler view addresses several issues that the
• It’s supported on ARM, x86, and MIPS architectures
• Built-in activity transitions take the user seamlessly and is fully 64-bit compatible. screen if desired by the user. Users may elect to existing widgets have.
from one state to another with beautiful, animated • ART improves app performance and responsiveness. allow none, some, or all notification content to be • It enforced a programming style that results in good
motion. Efficient garbage collection reduces the number and shown on a secure lock screen. performance.
• The material theme adds transitions for your duration of pauses for GC events, which fit • It also comes with default animations for removing
activities, including the ability to use shared visual comfortably within the v-sync window so your app and adding elements.
elements across activities. doesn’t skip frames.

RecyclerView Fragments Fragment Life Cycle Fragment Life Cycle


• RecyclerView allow to use different layout managers • A Fragment represents a behavior or a portion of user • Android fragments have their own life cycle very • nAttach() The fragment instance is associated with an
for positioning items. interface in a FragmentActivity. similar to an android activity. This section briefs activity instance.
• Recycler view uses a ViewHolder to store references to • You can combine multiple fragments in a single different stages of its life cycle. • The fragment and the activity is not fully initialized.
the views for one entry in the recycler view. activity to build a multi-pane UI and reuse a fragment Typically you get in this method a reference to the
• A ViewHolder class is a static inner class in your in multiple activities. activity which uses the fragment for further
adapter which holds references to the relevant views. • A fragment must always be hosted in an activity and initialization work.
• With these references your code can avoid the the fragment's lifecycle is directly affected by the host • onCreate() The system calls this method when
time-consuming findViewById() method to update the activity's lifecycle. creating the fragment.
widgets with new data. • You should initialize essential components of the
fragment that you want to retain when the fragment is
paused or stopped, then resumed.

Fragment Life Cycle Fragment Life Cycle Fragment Life Cycle


• onCreateView() The system calls this callback when • At this point, view can be accessed with the • onDestroyView() Fragment view will destroy after call
it's time for the fragment to draw its user interface for findViewById() method. this method
the first time. • onStart() The onStart() method is called once the • onDestroy() onDestroy() called to do final clean up of
• To draw a UI for your fragment, you must return fragment gets visible. the fragment's state but Not guaranteed to be called
a View component from this method that is the root of • onResume() Fragment becomes active. by the Android platform.
your fragment's layout. • onPause() The system calls this method as the first
• You can return null if the fragment does not provide a indication that the user is leaving the fragment.
UI. • This is usually where you should commit any changes
• onActivityCreated() The onActivityCreated() is called that should be persisted beyond the current user
after the onCreateView() method when the host session.
activity is created. • onStop() Fragment going to be stopped by calling
• Activity and fragment instance have been created as onStop().
well as the view hierarchy of the activity.

You might also like