Cosc 4735 Activities, fragments, callbacks/listeners/interfaces.

Slides:



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

Introduction to Java 2 Programming
Programming with Android: Android for Tablets Luca Bedogni Marco Di Felice Dipartimento di Scienze dell’Informazione Università di Bologna.
Object Oriented Programming
Web Application Development Slides Credit Umair Javed LUMS.
Inheritance Writing and using Classes effectively.
Cosc 5/4730 Android Services. What is a service? From android developer web pages: Most confusion about the Service class actually revolves around what.
Slides 4/22 COP Topics Final Exam Review Final Exam The final exam is Friday, April 29 th at 10:00 AM in the usual room No notes, books, calculators,
Cosc 5/4730 Material Design Spec’s and Toolbar.. Material Design First and for most, Material Design is a spec used by android to have app look similar.
Cosc 5/4730 Blackberry and Android: Menus. BLACKBERRY.
Cosc 4755 Phone programming: GUI Concepts & Threads.
Inheritance. © 2004 Pearson Addison-Wesley. All rights reserved 8-2 Inheritance Inheritance is a fundamental object-oriented design technique used to.
@2011 Mihail L. Sichitiu1 Android Introduction Hello Views Part 1.
Cosc 4730 Android TabActivity and ListView. TabActivity A TabActivity allows for multiple “tabs”. – Each Tab is it’s own activity and the “root” activity.
(c) University of Washington03-1 CSC 143 Java Inheritance Reading: Ch. 10.
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.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Cosc 5/4730 Introduction: Threads, Android Activities, and MVC.
Classes and Objects. Topics The Class Definition Declaring Instance Member Variables Writing Instance Member Methods Creating Objects Sending Messages.
CreatingClasses-part11 Creating Classes part 1 Barb Ericson Georgia Institute of Technology Dec 2009.
Create Navigation Drawer Team 2 Zhong Wang Jiaming Dong Philip Wu Lingduo Kong.
Effective C#, Chapter 1: C# Language Elements Last Updated: Fall 2011.
Programming Mobile Applications with Android September, Albacete, Spain Jesus Martínez-Gómez.
Android – Fragments L. Grewe.
Cosc 5/4730 Dialogs and below 3.0 and above (fragment)
E FFECTIVE C# 50 Specific Ways to Improve Your C# Second Edition Bill Wagner محمد حسین سلطانی.
Cosc 5/4730 Android Communications Intents, callbacks, and setters.
CSC1401 Classes - 1. Learning Goals Computing concepts Identifying objects and classes Declaring a class Declaring fields Default field values.
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.
Chapter 10 Defining Classes. The Internal Structure of Classes and Objects Object – collection of data and operations, in which the data can be accessed.
Inheritance & Dynamic Binding. Class USBFlashDrive We better introduce a new class USMBFlashDrive and save() is defined as a method in that class Computer.
CIS Intro to JAVA Lecture Notes Set July-05 GUI Programming – Home and reload buttons for the webbrowser, Applets.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
New Activity On Button Click via Intent. App->res->Layout->Blank Activity A new xml file is created and a new class is added to java folder. In manifest.xml.
Static. 2 Objectives Introduce static keyword –examine syntax –describe common uses.
ListView and ExpandableListView
Application development with Java Lecture 21. Inheritance Subclasses Overriding Object class.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 3 – Extending classes.
User Interface Layout Interaction. EventsEvent Handlers/Listeners Interacting with a user.
Advanced Java class Nested Classes & Interfaces. Types of Nested Classes & Interfaces top-level nested –classes –interfaces inner classes –member –local.
Mid-Year Review. Coding Problems In general, solve the coding problems by doing it piece by piece. Makes it easier to think about Break parts of code.
Events. Slide 2©SoftMoore Consulting Events Events are generated when a user interacts with the view objects of an application. Examples –button clicked–
Mobile Programming Lecture 4 Resources, Selection, Activities, Intents.
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,
Java for android Development Nasrullah Khan. Using instanceof in Android Development the classes such as Button, TextView, and CheckBox, which represent.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Cosc 5/4730 Support design library. Support Design library Adds (API 9+) back support to a number of 5.0 lollipop widgets and material design pieces –
Cosc 5/4730 RecyclerView And more..
Modern Programming Tools And Techniques-I
Reference: Object Oriented Design and Programming (Horstmann)
Web Design & Development Lecture 9
Classes and Objects.
CS240: Advanced Programming Concepts
Android – Event Handling
android architecture components with mvvm
ANDROID UI – FRAGMENTS UNIT II.
Inheritance "Question: What is the object oriented way of getting rich? Answer: Inheritance.“ “Inheritance is new code that reuses old code. Polymorphism.
null, true, and false are also reserved.
Android Lists and Fragments
Interfaces.
Android Topics Custom ArrayAdapters Creating an Event Listener
Android Topics Asynchronous Callsbacks
Constructors, GUI’s(Using Swing) and ActionListner
Android Developer Fundamentals V2
Subtype Substitution Principle
plus content providers, loaders, recyclerview
CS 240 – Advanced Programming Concepts
Chapter 11 Inheritance and Encapsulation and Polymorphism
Encapsulation.
Presentation transcript:

Cosc 4735 Activities, fragments, callbacks/listeners/interfaces

Activity – It’s main code of the display – It can easy change between different fragments Changing fragments from inside a fragment leads to “odd” behavior, avoid this. Fragment – An encapsulated display and code to power it. Including menus – How do I change a fragment then? Using a callback/interface/listener google uses these interchangeably, even though are differences.

Frag1 will call Frag1callback() in the activity do actions such as changing fragments, adding information to a “global list”, updating another fragment. – Note, the setter for each allows another fragment to update information via the activity using a callback. – Note 2, you can have more then one callback. The activity just needs to implemented both. – And final Note, you can use the same name in the all three fragments (example: fragcallback() ) and then activity implements one ( same name fragcallback() ).

Callback/interface vs listener A callback or interface must have an “implements X” otherwise it won’t compile or dies a runtime. – There is no default action A listener can work without any extra code and the default action is taken. Normally a default action is to do nothing.

Interface/callback Activity public class MyActivity extends Activity implements myFragment.OnFragmentInteractionCallback { public void onFragmentInteraction(String item) { whatever code/action that are needed } } //end of activity myFragment code private OnFragmentInteractionCallback mCallback; When the fragment calls the activity if (mCallback != null) { mCallback.onFragmentInteraction("0"); Setup public void onAttach(Activity activity) { mListener = (OnFragmentInteractionCallback) activity; public void onDetach() { super.onDetach(); mListener = null; } Interface and method(s) the activity must implement. In this case 1 method. public interface OnFragmentInteractionCallback { public void onFragmentInteraction(String list); }

Listener Activity public class MyActivity extends Activity { … myFrag.setListner(new myFragment.OnFragmentInteractionLister() { public void onFragmentInteraction(String item) { // whatever code/action that are needed } } //end of activity A note, The myFragment  Could just use if (mListener != null) instead of the dummyListener. myFragment code private OnFragmentInteractionListener mListener; In Constructor/onCreate mListener = dummyListener; //uses dummylistener. When the fragment calls the activity mListener.onFragmentInteraction("0"); Set method to change the listener. setListener(onFragmentInteractionListener listener) { mLister = listener; //now uses activities listener. } Interface and method(s) the activity must implement. In this case 1 method. public interface OnFragmentInteractionListener{ public void onFragmentInteraction(String list); } Dummy callback, so main is not required to do anything. Private OnFragmentInteractionListenerdummyListener = new OnFragmentInteractionListener() { public void onFragmentInteraction(String list) { //do nothing }

Callback/listener “hell” Somethings to get back to activity, you may need a callback that uses a listener (or several nested) Say getting from an adapter back to activity via a fragment. This is code needed to get back from recyclerView adapter to the activity. Say when a user clicks a button.

Adapter code first public class myRecyclerCursorAdapter extends CursorRecyclerAdapter { // Define listener member variable for the adapter. private OnItemClickListener listener; // Define the listener interface public interface OnItemClickListener { void onItemClick(View itemView, String id); } // Define the method that allows the parent activity or fragment to define the listener public void setOnItemClickListener(OnItemClickListener listener) { this.listener = listener; public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.trans_row, parent, false); ViewHolder vh = new ViewHolder(itemView, listener); return vh; } public static class ViewHolder extends RecyclerView.ViewHolder { public TextView mName; public Button btn; public ViewHolder(View view, final OnItemClickListener mlistener) { super(view); … Btn.setOnClickListener(new View.OnClickListener() public void onClick(View v) { // Triggers listner upwards to the adapter. if (mlistener != null) mlistener.onItemClick(v, mName.getTag().toString()); } }); } } //end of static class ViewHolder }//end of class

Fragment and Activity Code Fragment, skipping attach/detach code. private OntransInteractionCallback mCallback; myRecyclerCursorAdapter mAdapter; In onCreateView mAdapter.setOnItemClickListener(new myRecyclerCursorAdapter.OnItemClickListener() { //remember this listener is going to the public void onItemClick(View view, String id) { mCallback.ontransInteraction( …); } }); Rest of Fragment piece. public interface OntransInteractionCallback { public void ontransInteraction(Uri uri); } Activity public class mActivity extends Activity implements myFragment. OntransInteractionCallback Somewhere in the public void ontransInteraction(…) { //finally deal with the information from the adapter! }

Q A &