Class on Fragments & threads. Fragments fragment is a modular section of an activity, which has its own lifecycle, receives its own input events, and.

Slides:



Advertisements
Similar presentations
Programming with Android: Android for Tablets Luca Bedogni Marco Di Felice Dipartimento di Scienze dellInformazione Università di Bologna.
Advertisements

Bruce Scharlau, University of Aberdeen, 2010 Android UI, and Networking Mobile Computing Based on android-sdk_2.2 Unless otherwise stated, images are from.
Programming with Android: Android for Tablets Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Activity Applications were originally built on the Activity class. AD * : “An activity is a single, focused thing that the user can do. Almost all activities.
Fragments: Introduction Fragments were introduced in Android 3.0 to support flexible and dynamic UI designs represent portions of an application’s user.
The Android Activity Lifecycle. Slide 2 Introduction Working with the Android logging system Rotation and multiple layouts Understanding the Android activity.
Threads, AsyncTasks & Handlers.  Android implements Java threads & concurrency classes  Conceptual view  Parallel computations running in a process.
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
Android Fragments.
Multithreading in Java Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Multithreading.
Programming with Android: Android Fragments Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Android UI, and Networking. Can do most networking on Android Bluetooth only on 2.0, Not supported with version 1.6.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
ANDROID UI – FRAGMENTS. Fragment  An activity is a container for views  When you have a larger screen device than a phone –like a tablet it can look.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Concurrency in Android with.
Chapter 2: Simplify! The Android User Interface
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Networking: Part 2 (Accessing the Internet). The UI Thread When an application is launched, the system creates a “main” UI thread responsible for handling.
Chapter 2 The Android User Interface. Objectives  In this chapter, you learn to:  Develop a user interface using the TextView, ImageView, and Button.
SpotOn Game App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Android Boot Camp for Developers Using Java, 3E
Android – Fragments L. Grewe.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 2: Simplify! The Android User Interface.
1 Web Based Programming Section 8 James King 12 August 2003.
CSC 205 – Java Programming II Applet. Types of Java Programs Applets Applications Console applications Graphics applications Applications are stand-alone.
Services A Service is an application component that can perform long-running operations in the background and does not provide a user interface. An application.
Cosc 4730 Android Fragments. Fragments You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own.
Threads II IS Outline  Quiz  Thread review  Stopping a thread  java.util.Timer  Swing threads javax.swing.Timer  ProgressMonitor.
HW#9 Clues CSCI 571 Fall, HW#9 Prototype
Multithreaded programming  Java provides built-in support for multithreaded programming. A multithreaded program contains two or more parts that can run.
Activities Димитър Н. Димитров Astea Solutions AD.
Lecture 6: Process and Threads Topics: Process, Threads, Worker Thread, Async Task Date: Mar 1, 2016.
CHAPTER 4 Fragments ActionBar Menus. Explore how to build applications that use an ActionBar and Fragments Understand the Fragment lifecycle Learn to.
CHAPTER 6 Threads, Handlers, and Programmatic Movement.
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,
The Ingredients of Android Applications. A simple application in a process In a classical programming environment, the OS would load the program code.
Android Fragments. Slide 2 Lecture Overview Getting resources and configuration information Conceptualizing the Back Stack Introduction to fragments.
The Flag Quiz app tests your ability to correctly identify 10 flags from various countries and territories.
1. 2 The Address Book app provides convenient access to contact information that’s stored in a SQLite database on the device. You can: scroll through.
Wiring Fragments Wiring Dialogs. Ask FragmentManager whether home Fragment exists If not, create home Fragment Add it Main Activity setContentView Launch.
Fragments and Menus Chapter 4 1. Objectives Learn three different types of menus: options, context, and popup Learn to configure the ActionBar and Toolbar.
Chapter 2: Simplify! The Android User Interface
Concurrency in Android
Small talk with the UI thread
Activity and Fragment.
Activities, Fragments, and Events
Fragment ?.
Fragments: Introduction
Mobile Application Development BSCS-7 Lecture # 6
Android 16: Fragments: Tutorial
Mobile Applications (Android Programming)
Java Concurrency.
Activities and Intents
Widgets & Fragments Kalin Kadiev Astea Solutions AD.
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
The Android Activity Lifecycle
Android – Fragments L. Grewe.
ANDROID UI – FRAGMENTS UNIT II.
Threads II IS
CIS 470 Mobile App Development
Chapter 9: Fragments.
Cannon Game App Android How to Program
Android Topics UI Thread and Limited processing resources
Android Topics Asynchronous Callsbacks
Android Topics Threads and the MessageQueue
CIS 470 Mobile App Development
Mobile Programming Dr. Mohsin Ali Memon.
Activities and Fragments
Android Sensor Programming
Presentation transcript:

Class on Fragments & threads

Fragments fragment is a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running. Flexibility in designing the UI that single screen it is possible to accommodate more than one UI modules. Activity act as a container & host multiple fragments to show on a single screen.

Building Fragments 3 steps Designing the Fragment layout. Creating fragment class. Associating fragment to an activity.

To start with.. Design the layout of the fragment in the associated layout XML file. If 2 fragment in an XML file-define 2 layout file.  Step 2 Create a class that extend fragment. This class is required to inflate the fragment layout & define the behavior of Fragment. To create a fragment, extend the Fragment class, then override key lifecycle methods to insert your app logic, similar to the way you would with an Activity class.

One difference when creating a Fragment is that you must use the onCreateView() callback to define the layout. This is the only callback you need in order to get a fragment running. Example

Just like an activity, a fragment should implement other lifecycle callbacks that manage its state as it is added or removed from the activity and as the activity transitions between its lifecycle states. For instance, when the activity's onPause() method is called, any fragments in the activity also receive a call to onPause().

Add a Fragment to an Activity using XML While fragments are reusable, modular UI components, each instance of a Fragment class must be associated with a parent FragmentActivity. You can achieve this association by defining each fragment within your activity layout XML file.

Then apply the layout to your activity:

When you add a fragment to an activity layout by defining the fragment in the layout XML file, you cannot remove the fragment at runtime. If you plan to swap your fragments in and out during user interaction, you must add the fragment to the activity when the activity first starts.

Fragments can be dynamically associated with an Activity. It is achieved by placing them in the layout file of the activity using tag. Here in the onResume() method of the Activity,we attach the required Fragments at runtime using the FragmentManager API.

FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction(); fragmentTransaction.add(R.id.namefragmentContainer,new NameFragment(),”tagNationFragment”); fragmentTransaction.commit(); Parameters of add()method-id of the placeholder for the fragment,instance of the Fragment & a tag with which the Fragment can be referred later if needed.

Life Cycle of Fragments The difference between the lifecycle of an activity and that of a fragment is Activity lifecycle is managed by Android runtime using task stack. Fragments are managed by the host activity using back stack mechanism. The back stack mechanism helps developer to manage multiple Fragment transactions in an Activity.

Design and implement long running task using Thread.Example A scenario where an Activity allows a user to start,stop and reset a counter. When the counter is running,its current value has to be continuously displayed in the TextView. If increment of counter in onClick() method,andstart the counter,subsequent tap on stop or reset result in ANR. To overcome this,need to offload the counter increment logic to a separate thread

Public void onClick(View arg0) { Switch(arg0.getId() { Case R.id.startCounterButton: keepCounting=true; New Thread(new Runnable() Public void run() { While (keepCounting) { Try { Thread.sleep(1000); } catch (InterruptedException e) { E.printStackTrace(); } Count++; displayValue.post(new Runnable() Public void run() { displayValue.setText(count +””); } }); } }).start(); Break; Case R.id.stopCounterButton: keepCounting=false; Break; Case R.id.resetButton: Count=0; displayValue.setText(count + “”); Break; }

To access a thread from a non UI thread Get hold of the main thread(Accomblish by a handler). A handler is an object which always refer to the thread that created it. The handler of the main thread allow other threads to post new tasks to a message queue maintain by the main thread. This message queue is used to line up tasks,including UI updates. Task in this queue are executed on a first come first serve basis. Once the message queue is exhausted, the main thread waits for a new task(s) in an endless loop. This is initiated by looper. When the post() method is invoked,the non-UI thread post a task to the message queue of the main thread by obtaining the handler of main thread.