Communication between Activities

Slides:



Advertisements
Similar presentations
Android Application Development Tutorial. Topics Lecture 6 Overview Programming Tutorial 3: Sending/Receiving SMS Messages.
Advertisements

Manifest File, Intents, and Multiple Activities. Manifest File.
The Activity Class 1.  One application component type  Provides a visual interface for a single screen  Typically supports one thing a user can do,
Hello world Follow steps under the sections “Create an AVD” and “Create a New Android Project” at
Application Fundamentals. See: developer.android.com/guide/developing/building/index.html.
@2010 Mihail L. Sichitiu1 Android Introduction Hello Views Part 2.
@2011 Mihail L. Sichitiu1 Android Introduction Communication between Activities.
@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.
Android Development (Basics)
@2011 Mihail L. Sichitiu1 Android Introduction Hello World.
Android Application Development Tutorial. Topics Lecture 5 Overview Overview of Networking Programming Tutorial 2: Downloading from the Internet.
Chien-Chung Shen Manifest and Activity Chien-Chung Shen
Getting Started with Android APIs Ivan Wong. Motivation - “Datasheet” - Recently exposed to what’s available in Android - So let’s see what API’s are.
Mobile Programming Lecture 9 Bound Service, Location, Sensors, IntentFilter.
Mobile Programming Lecture 16 The Facebook API. Agenda The Setup Hello, Facebook User Facebook Permissions Access Token Logging Out Graph API.
Hello world Follow steps under the sections “Create an AVD” and “Create a New Android Project” at
Chapter 2: Simplify! The Android User Interface
Android Activities 1. What are Android Activities? Activities are like windows in an Android application An application can have any number of activities.
1 Announcements Homework #2 due Feb 7 at 1:30pm Submit the entire Eclipse project in Blackboard Please fill out the when2meets when your Project Manager.
@2011 Mihail L. Sichitiu1 Android Introduction GUI Menu Many thanks to Jun Bum Lim for his help with this tutorial.
Chapter 2 The Android User Interface. Objectives  In this chapter, you learn to:  Develop a user interface using the TextView, ImageView, and Button.
Android Programming-Activity Lecture 4. Activity Inside java folder Public class MainActivity extends ActionBarActivity(Ctrl + Click will give you the.
Understand applications and their components activity service broadcast receiver content provider intent AndroidManifest.xml.
Android Boot Camp for Developers Using Java, Comprehensive: A Guide to Creating Your First Android Apps Chapter 2: Simplify! The Android User Interface.
Networking: Part 1 (Web Content). Networking with Android Android provides A full-featured web browser based on Chromium, the open source browser engine.
Linking Activities using Intents How to navigate between Android Activities 1Linking Activities using Intents.
로봇 모니터링 1/2 UNIT 20 로봇 SW 콘텐츠 교육원 조용수. 학습 목표 Message Queue Handler 2.
Activity Android Club Agenda Hello Android application Application components Activity StartActivity.
Android Boot Camp Demo Application – Part 1. Development Environment Set Up Download and install Java Development Kit (JDK) Download and unzip Android.
Mobile Programming Midterm Review
1 CMSC 628: Introduction to Mobile Computing Nilanjan Banerjee Introduction to Mobile Computing University of Maryland Baltimore County
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.
Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.
Activities and Intents Richard S. Stansbury 2015.
Intents 1 CS440. Intents  Message passing mechanism  Most common uses:  starting an Activity (open an , contact, etc.)  starting an Activity.
Android and s Ken Nguyen Clayton state University 2012.
Android Alert Dialog. Alert Dialog Place Button to open the dialog. public class MainActivity extends ActionBarActivity { private static Button button_sbm;
Mobile Programming Lecture 4 Resources, Selection, Activities, Intents.
Android 基本 I/O. 基本 I/O 介面元件 在此節中主要介紹常見的 I/O 使用者介 面元件 – Button, TextView, 以及 EditText , 學習者可以學會: – Android 的視窗表單設計 res/layout/main.xml – Android SDK –
School of Engineering and Information and Communication Technology KIT305/KIT607 Mobile Application Development Android OS –Permissions (cont.), Fragments,
CMPE419 Mobile Application Development Asst.Prof.Dr.Ahmet Ünveren SPRING Computer Engineering Department Asst.Prof.Dr.Ahmet Ünveren
Chapter 2: Simplify! The Android User Interface
Lab7 – Appendix.
Lab7 – Advanced.
Android N Amanquah.
several communicating screens
GUI Programming Fundamentals
Android – Event Handling
Android Introduction Hello World.
CS499 – Mobile Application Development
Android Widgets 1 7 August 2018
Android – Read/Write to External Storage
Android Introduction Camera.
CIS 470 Mobile App Development
MultiUni Trần Vũ Tất Bình
CIS 470 Mobile App Development
CIS 470 Mobile App Development
Many thanks to Jun Bum Lim for his help with this tutorial.
Activities and Intents
Activities and Intents
CIS 470 Mobile App Development
CIS 470 Mobile App Development
ארועים ומאזינים android.
BLP 4216 MOBİL UYGULAMA GELİŞTİRME-2
Objects First with Java
CMPE419 Mobile Application Development
Activities, Fragments, and Intents
CIS 694/EEC 693 Android Sensor Programming
CIS 694/EEC 693 Android Sensor Programming
Presentation transcript:

Communication between Activities Android Introduction Communication between Activities

Goal Create an application that has 3 Activities: a main activity and two sub-activities Introduce two different methods for inter-activity communication: message return and static variable <via static valuables> <via message> Invoke Activity Invoke Activity Read from the shared space Return result Change values in shared space Shared valuables <Main Activity>

Overview Create two new sub-activities (in addition to the main activity) Create the two classes (.java) Create the two layouts (.xml) Invoke the two new sub-activities Have the sub-activities return results Read the results of the sub-activities from the main activity

Create the new sub-activities File > New > Class Copy & Paste the “OnCreate()” from the main activity – change the layout Copy & Paste the main.xml to two new layouts Change all three layouts

Invoke Sub-activities from the Main Activity private static final int INTENT_GET_MSG = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); staticMsgTextView = (TextView)findViewById(R.id.textViewStaticMsg); invokeButton = (Button)findViewById(R.id.invokeButton); invokeButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { Intent msgActivityIntent = new Intent(ActivityInteractionActivity.this, AdditionalActivity.class); startActivityForResult(msgActivityIntent, INTENT_GET_MSG); } }); invokeStaticButton = (Button)findViewById(R.id.invokeStaticButton); invokeStaticButton.setOnClickListener(new OnClickListener() { Intent staticActivityIntent = new Intent(ActivityInteractionActivity.this, StaticMsgActivity.class); startActivity(staticActivityIntent); Name of your main activity class for interaction via message return for interaction via static valuables

Interaction via message return - Invoke button clicked ! startActivityForResult(msgActivityIntent, INTENT_GET_MSG); - OK button clicked ! - Intent intent = new Intent(); - intent.putExtra(RETURN_MSG, msg); setResult(Activity.RESULT_OK, intent); finish(); Build an intent to return the result message public void onActivityResult() { : case INTENT_GET_MSG: String returnMsg = data.getExtras() .getString(AdditionalActivity.RETURN_MSG); } Receive the message <Main Activity> <Sub Activity>

Interaction via message return Sub-activity for message return When ok button clicked, return to the main Activity with result message via intent. public Static String RETURN_MSG = “return_msg”; @Override public void onCreate(Bundle savedInstanceState) { : okButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { String msg = editText.getText().toString(); Intent intent = new Intent(); intent.putExtra(RETURN_MSG, msg); // Set result and finish this Activity setResult(Activity.RESULT_OK, intent); // The ActivityResult is propagated back to main activity // via onActivityResult(). finish(); } });

Interaction via message return Main Activity can receive the result message from sub-activity by overriding onActivityResult() @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { switch (requestCode) { case INTENT_GET_MSG: if (resultCode == Activity.RESULT_OK ) { String returnMsg = data.getExtras() .getString(AdditionalActivity.RETURN_MSG); Toast.makeText(this, returnMsg , Toast.LENGTH_SHORT).show(); } else { Toast.makeText(this, "Error !! ", Toast.LENGTH_SHORT).show(); } break; }// end switch

Interaction via static variables - Invoke button clicked ! startActivity(staticActivityIntent); - OK button clicked ! - StaticStorage.msg = msg; Save result in a static variable staticMsgTextView.setText(StaticStorage.msg ); Read the static variable <Main Activity> <Sub Activity>

Interaction via static variables By defining a public static variable, both Activities can share the static variables. Create a new class we call StaticStorage (File>New>Class) – same package as the other java files. package ncsu.summer.android; public class StaticStorage { public static String msg = null; }

Interaction via static variables Sub-activity using static variables When ok button clicked, save the result message in the static variable(s) shared between Activities. @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.static_msg_activity); editText = (EditText)findViewById(R.id.editText); okButton = (Button)findViewById(R.id.okButton); okButton.setOnClickListener(new OnClickListener() { public void onClick(View v) { String msg = editText.getText().toString(); StaticStorage.msg = msg; // Save result in a static variable finish(); } });

Interaction via static variables Main Activity access the static variable in onResume() @Override public void onResume() { super.onResume(); if( StaticStorage.msg != null && staticMsgTextView != null){ staticMsgTextView.setText( StaticStorage.msg ); }

AndroidManifest.xml Add two sub-activities in AndroidManifest.xml <activity android:name=".AdditionalActivity" android:label="@string/app_additional_activity"> </activity> <activity android:name=".StaticMsgActivity" android:label="@string/app_static_msg_activity">

strings.xml Add activity names used in AndroidManifest.xml <string name="app_additional_activity">Additional Activity</string> <string name="app_static_msg_activity">Static Msg Activity</string>