Download - Lecture #3 activities and intents
![Page 1: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/1.jpg)
Android AcademySession #3
Yonatan Levin
Activities and Intents3
![Page 2: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/2.jpg)
First,
![Page 3: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/3.jpg)
Yonatan Levin
levin.yonatanparahall
![Page 4: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/4.jpg)
Campus TLVCampus TLV
Campus TLV
Campus TLV
![Page 5: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/5.jpg)
~ 2000 members Largest Android Active Community
![Page 6: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/6.jpg)
Jonathan Yarkoni
Android Developer & Advocate Ironsource
Android Academy Staff
Yonatan LevinGoogle Developer
Expert & Android @ Gett
Britt BarakAndroid Lead
Figure8
Yossi SegevAndroid Developer
Crave
![Page 7: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/7.jpg)
What Do We Do?
●Android Fundamentals
●Android UI / UX
●Community Hackathon
●Android Performance
●Mentors Program●Active community
![Page 8: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/8.jpg)
Online Lessons
Important:
Watch online lesson
before the meetup!
- Our course: “Developing
Android Apps”goo.gl/u1pxZv
- Optional: Nano Degree
- Optional: “Android Basics” courses
![Page 9: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/9.jpg)
The Course Plan
- Online lesson @ home!
- Lecture @ Campus- Hands-on @ Campus- Questions @ Facebook
![Page 10: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/10.jpg)
facebook.com/groups/android.academy.ils
![Page 11: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/11.jpg)
![Page 12: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/12.jpg)
![Page 13: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/13.jpg)
Community Mentors
Roman Smirnov
![Page 14: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/14.jpg)
Questions ?
![Page 15: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/15.jpg)
What’s For Today?● Listeners● Toasts ● Intents, StartActivity,
Navigation● SharedPreferences● Broadcast Receivers
![Page 16: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/16.jpg)
I ❤ Today’s Lecture!In this lecture, besides the Android stuff, we will show-case 2 design patterns:
Hidden Agenda for Today
The Observer PatternThe Static Factory Method Pattern
![Page 17: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/17.jpg)
Design Patterns will be in Purple slides.
Design patterns will get this background. If you don’t know design patterns, it’s never too late to learn.
![Page 18: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/18.jpg)
Sources
![Page 19: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/19.jpg)
a CLASSic
A Java Moment-
![Page 20: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/20.jpg)
OuterClass.InnerClass innerObject =
outerObject.new
InnerClass();
Consider this Crazy java codeline
![Page 21: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/21.jpg)
Nested Classes
In Java, a class (or interface) can be declared inside another class.class A{ class B{ // ... }}
Read more: https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
Inside C, you can use A.B:class C{ A.B abMember; A.B doSomething(){ /* … */ }}
![Page 22: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/22.jpg)
Nested Classes
There are 2 types of nested classes: class A{
// This one is called a static nested class. static class B{ // ... }
// This one is called an inner class - because there’s no static. class B{ // ... }}Read more: https://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html
![Page 23: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/23.jpg)
Nested Classes - a Map
Read more: https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
Nested Class
Static Nested Class
Inner Class
Local class Anonymous Class
Examples:ViewHolderAsyncTask
Examples:RunnableOnClick
![Page 24: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/24.jpg)
OuterClass.InnerClass innerObject =
outerObject.new
InnerClass();
Consider this Crazy java codeline
![Page 25: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/25.jpg)
Our Starting Point
in activity_main.xml: <Button android:text="Hooking to buttons" android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="goToButtonsDemo" />
In MainActivity.java:public void goToButtonsDemo(View view) { Intent i = new Intent(this, ButtonsDemoActivity.class); startActivity(i);}
![Page 26: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/26.jpg)
Handling Events
3 ways to listen(er)s
![Page 27: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/27.jpg)
Way #1: android:onClick=”...” in xml
In activity_buttons_demo.xml declare the button with an OnClick: <Button android:text="Red +" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:onClick="increaseRed" android:background="@color/lightred" />
In ButtonsDemoActivity.java, implement the method that will be calledpublic void increaseRed(View view) { // ...}
![Page 28: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/28.jpg)
Way #2: Inline OnClickListener
In the Activity’s onCreate method, grab the buttons and set their OnClickListener with an inline implementation@Overrideprotected void onCreate(Bundle savedInstanceState) { // ... Button increaseGreen = (Button)findViewById(R.id.buttonsdemo_incGreen); increaseGreen.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // ... } });}
![Page 29: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/29.jpg)
Way #3: Single OnClickListener
(1) Make the activity implement View.OnClickListener: public class ButtonsDemoActivity extends ActionBarActivity implements View.OnClickListener
(2) In OnCreate, set the button’s listener to the activity (with this):@Overrideprotected void onCreate(Bundle savedInstanceState) { // … Button decreaseRed = (Button)findViewById(R.id.buttonsdemo_decRed); decreaseRed.setOnClickListener(this);}
![Page 30: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/30.jpg)
Way #3: Single OnClickListener
(3) Implement the interface, use a switch to tell which button view was clicked:@Overridepublic void onClick(View v) { switch (v.getId()){ case R.id.buttonsdemo_decRed: red = calcNewValue(red, -1); break; case R.id.buttonsdemo_decGreen: green = calcNewValue(green, -1); break; case R.id.buttonsdemo_decBlue: blue = calcNewValue(blue, -1); break; } refreshDisplay();}
![Page 31: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/31.jpg)
Demo: The Color Mixer
1 Button: Way #12 Buttons: Way #23 Buttons: Way #3
![Page 32: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/32.jpg)
Not really 3 ways...
In fact, there’s only one way to have a button do something:
Set a View.OnClickListener.Way #1 does this implicitly, and ways 2,3 does this explicitly.
![Page 33: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/33.jpg)
What is a listener?
Listener: an interface that contains a single callback method.
When the user interacts with the UI, the Views trigger a call to the listener (if it’s not null) and calls the callback.As long as the view and the listener agrees on the contract between them (~ The method’s signature), the view doesn’t care what the implementation is.This is a great example of the Observer design pattern.Source: http://developer.android.com/guide/topics/ui/ui-events.html#EventListeners
![Page 34: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/34.jpg)
The Observer Design Pattern
A Behavioral design pattern, commonly used in UI (but not only).
Read a lot more: https://en.wikibooks.org/wiki/Computer_Science_Design_Patterns/Observer
View.OnClickListener
View
Your Listener
![Page 35: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/35.jpg)
How does xml onClick (way #1) work
When you use the xml’s onClick way, the View uses a View.DeclaredOnClickListener, It uses reflection (which is slow on android),but It’s Lazy and Cached,and is not validated at Compile-Time.Since android is Open-Source, check out the implementation at the link below.https://github.com/android/platform_frameworks_base/blob/master/core/java/android/view/View.java Line 4429
![Page 36: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/36.jpg)
Which is better?
Way #1: onClick=”...” → DeclaredOnClickListener
Way #2: inline listener implementation
Way #3: activity interface implementationhttp://stackoverflow.com/questions/21319996/android-onclick-in-xml-vs-onclicklistener
Pros: Clean code, no findViewById at allCons: Reflection, No Compile-Time validation, API Level ≥ 4, not readable
Pros: Readable codeCons: +1 class, +1 method, ~500 bytes, +Boilerplate code, if you have a lot of clicks becomes mess
Pros: No object allocationsCons: Felix: The horrible switch (over view.getId())
![Page 37: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/37.jpg)
What other listeners are available?
Read more: http://developer.android.com/guide/topics/ui/ui-events.html
Event HandleronClickView.OnClickListener onLongClickView.OnLongClickListeneronFocusChangeView.OnFocusChangeListeneronKeyView.OnKeyListeneronTouchView.OnTouchListeneronCreateContextMenuView.OnCreateContextMenuListener
AdapterView.OnItemClickListener
![Page 38: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/38.jpg)
Non-void Listeners
Some flows requires listeners to tell when they take care of the event.These listeners return a boolean:true if the event is consumed, or false if it isn’t.onTouchListener is a good example.
![Page 39: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/39.jpg)
OnClick || OnTouch?
OnTouch gets called on any change in the touch detection,and contains touch data. The MotionEvent is passed to the root view, and it travels until it finds an OnTouchListener that would handle it. Some may react to the event without consuming it - common practice for when using Gesture Detectors.For “Everyday tapping” - You usually should go with onClick.Read More: http://codetheory.in/understanding-android-input-touch-events/
and here: http://developer.android.com/training/gestures/detector.html
![Page 40: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/40.jpg)
Take Aways - Listeners
1.UI Interaction is implemented with Listeners.
2.Some listeners returns a boolean value to indicate handling.
3.OnTouch is really important, and in most cases, you won’t need it.
![Page 41: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/41.jpg)
And the Static Factory Method pattern
ToastsB
![Page 42: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/42.jpg)
Toasts
provides simple feedback about an operation in a small popup. The current activity remains visible and interactive - But the toast itself is not interactive.Toast disappear after a short time.
http://developer.android.com/guide/topics/ui/notifiers/toasts.html
![Page 43: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/43.jpg)
Make a Toast
Toast.makeText(this,"I'm your father,Luke!",Toast.LENGTH_LONG)
.show();
![Page 44: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/44.jpg)
Toast Positioning
toast.setGravity(Gravity.CENTER|Gravity.RIGHT, 0, 0);
![Page 45: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/45.jpg)
Static Factory Method
●The best ways to create an object.●Have a name●Can cache and not always create new class●They return an object●Reduce the verbosity of creating parameterized
type instancesRead more: http://www.informit.com/articles/article.aspx?p=1216151
Hear more: http://fragmentedpodcast.com/episodes/14/
![Page 46: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/46.jpg)
Static Factory Methodpublic static Toast makeText(Context context, CharSequence text, @Duration int duration) { Toast result = new Toast(context);
LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View v = inflate.inflate(com.android.internal.R.layout.transient_notification, null); TextView tv = (TextView)v.findViewById(com.android.internal.R.id.message); tv.setText(text); result.mNextView = v; result.mDuration = duration;
return result;}
Read more: http://www.informit.com/articles/article.aspx?p=1216151
![Page 47: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/47.jpg)
Most important thing about Toasts:
show()
![Page 48: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/48.jpg)
Not in course: Snackbars
Defined in the Material Design spec, SnackBars also allow user interaction - and also work great with FABs - Both are out-of-scope from our course.
http://developer.android.com/reference/android/support/design/widget/Snackbar.html
![Page 49: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/49.jpg)
If you need user interaction, you can use a notification (which we’ll see in Session #6)
and if you really need a custom toast design, you can - but it’s not covered here.
Also not talking about...
![Page 50: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/50.jpg)
- It lets newly created objects understand what has been going on.
- Global information about an application environment- Creating New objects: Creating new views, adapters,
listeners:TextView tv = new TextView(getContext());ListAdapter adapter = new SimpleCursorAdapter(getApplicationContext(), ...);
- Accessing Standard Common Resources: Services like LAYOUT_INFLATER_SERVICE, SharedPreferences:context.getSystemService(LAYOUT_INFLATER_SERVICE);getApplicationContext().getSharedPreferences(*name*, *mode*);
- Accessing Components Implicitly: Regarding content providers, broadcasts, intent: getApplicationContext().getContentResolver().query(uri, ...);
Context - #1 reason of memory leaks
- Further reads: https://possiblemobile.com/2013/06/context/
![Page 51: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/51.jpg)
View svButton = findViewById(R.id.sv_button);svButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { setStaticView(); }});
![Page 52: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/52.jpg)
public static View view;
void setStaticView() { view = findViewById(R.id.sv_button);}
![Page 53: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/53.jpg)
![Page 54: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/54.jpg)
and also a bit about Navigation
Intents and StartActivity
C
![Page 55: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/55.jpg)
Intents
messaging object you can to request an action from another app component.Main Use-cases:
- Starting an activity- Broadcasting a message- Starting a service (wait for
Session 6)http://developer.android.com/guide/components/intents-filters.html
![Page 56: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/56.jpg)
Intent Types
Explicit Intent - have a ComponentName - so Android knows exactly what to call.
Implicit Intent, doesn’t have a ComponentName - so Android uses Intent Filters to know what to do.
![Page 57: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/57.jpg)
used for Explicit Intents
used for Implicit Intents
used to tell things to the recipient
used to tell things to the messenger
Intents have...
Component NameActionData (and Type)CategoryExtrasFlags
![Page 58: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/58.jpg)
Intent Filters
Intent filters are used to advertise what intents your app can handle. We’ve already seen MainActivity’s intent filter in Session #1.
Read more: http://developer.android.com/guide/components/intents-filters.html#Receiving
![Page 59: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/59.jpg)
ExplicitlyIntent intent = new Intent(this, TargetActivity.class);
startActivity(intent);
or
Intent intent = new Intent(getApplicationContext(), TargetActivity.class);
getApplicationContext().startActivity(intent);
![Page 60: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/60.jpg)
ImplicitlyIntent intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:37.7749,-122.4194"));
startActivity(intent);
![Page 61: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/61.jpg)
But what if?public class MainActivity extends AppCompatActivity {
public static final String TARGET_ACTIVITY_DATA_KEY = "KEY1"; public static final String TARGET_ACTIVITY_MORE_DATA_KEY = "KEY2"; public static final String TARGET_ACTIVITY_EVEN_MORE_DATA_KEY = "KEY3";
@Override protected void onCreate(Bundle savedInstanceState) { Intent intent = new Intent(this, TargetActivity.class); intent.putExtra(TARGET_ACTIVITY_DATA_KEY,"Luke Skywalker"); intent.putExtra(TARGET_ACTIVITY_MORE_DATA_KEY,"Darth Vader"); intent.putExtra(TARGET_ACTIVITY_EVEN_MORE_DATA_KEY,"Han Solo"); startActivity(intent); }
![Page 62: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/62.jpg)
But what if?public class TargetActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_target);
Bundle extras = getIntent().getExtras(); String first = (String) extras.get(MainActivity.TARGET_ACTIVITY_DATA_KEY); String second = extras.getString(MainActivity.TARGET_ACTIVITY_MORE_DATA_KEY); int error = extras.getInt(MainActivity.TARGET_ACTIVITY_EVEN_MORE_DATA_KEY); initViews(first,second,error); }
![Page 63: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/63.jpg)
What wrong?
- Non readable- Mix of static variables- Other activities should know what target expect- Run-time error
![Page 64: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/64.jpg)
Solutionpublic class MainActivity extends AppCompatActivity {
@Override protected void onCreate(Bundle savedInstanceState) {
Intent intent = TargetActivity .createIntent(this, "Luke Skywalker", "Darth Vader", "Han Solo"); startActivity(intent);
}}
![Page 65: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/65.jpg)
Solutionpublic class TargetActivity extends AppCompatActivity {
public static final String TARGET_ACTIVITY_DATA_KEY = "KEY1"; public static final String TARGET_ACTIVITY_MORE_DATA_KEY = "KEY2"; public static final String TARGET_ACTIVITY_EVEN_MORE_DATA_KEY = "KEY3";
public static Intent createIntent(Context context,String first,String second, String third){ Intent intent = new Intent(context, TargetActivity.class); intent.putExtra(TARGET_ACTIVITY_DATA_KEY,first); intent.putExtra(TARGET_ACTIVITY_MORE_DATA_KEY,second); intent.putExtra(TARGET_ACTIVITY_EVEN_MORE_DATA_KEY,third); return intent; }
![Page 66: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/66.jpg)
SharedPreferences SettingsActivity
D
![Page 67: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/67.jpg)
SharedPreferences
- Great thing to store persistent various. - Not accessible by other apps*- Used to save things between sessions- Sometimes could be great communication tool
(like intent) using “onSharedPreferenceChangeListener”
![Page 68: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/68.jpg)
MainActivitypublic static final String TEXT_SHARED_PREF_KEY = "TEXT_SHARED_PREF_KEY";public static final String SHARED_PREF_KEY = "KEY_FOR_SHARED_PREF";
@Overrideprotected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
Button saveBtn = (Button) findViewById(R.id.btn_am_save_data); Button restoreBtn = (Button) findViewById(R.id.btn_am_restore_data); saveBtn.setOnClickListener(this); restoreBtn.setOnClickListener(this);}
![Page 69: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/69.jpg)
MainActivity@Overridepublic void onClick(View v) { SharedPreferences preferences = getSharedPreferences(SHARED_PREF_KEY, MODE_PRIVATE); EditText editText = (EditText) findViewById(R.id.et_am_text); switch (v.getId()) { case R.id.btn_am_save_data: SharedPreferences.Editor editor = preferences.edit(); editor.putString(TEXT_SHARED_PREF_KEY, editText.getText().toString()); editor.apply(); finish(); break; case R.id.btn_am_restore_data: String text = preferences.getString(TEXT_SHARED_PREF_KEY,""); editText.setText(text); break; }}
![Page 70: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/70.jpg)
And...
- Don’t forget that each commit() is I/O operation.- use Apply() instead
![Page 71: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/71.jpg)
Last Part:Broadcast Receivers
E
![Page 72: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/72.jpg)
Broadcast Receivers
Great guy :)- Loose Coupling- 1-to-n relationship- The onReceive() method is always executed on the
main thread- You can notify components in your entire
application, so the communicating components do not have to "see" each other.
![Page 73: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/73.jpg)
![Page 74: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/74.jpg)
But...
- Marshaling data via intent really hard- Register/Unregister it when you do not needed him
(BaseActivity)- Not build to transfer large objects
![Page 75: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/75.jpg)
Receiverpublic class ResponseReceiver extends BroadcastReceiver { public static final String ACTION_RESP = "com.example.intent.action.PROGRESS_DOWNLOAD";
@Override public void onReceive(Context context, Intent intent) { TextView result = (TextView) findViewById(R.id.tv_am_progress); String text = intent.getStringExtra(DownloadService.PROGRESS); result.setText(text + "%"); }}
![Page 76: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/76.jpg)
Receiver@Overridepublic void onClick(View v) { Intent intent = new Intent(this, DownloadService.class); startService(intent); IntentFilter filter = new IntentFilter(ResponseReceiver.ACTION_RESP); filter.addCategory(Intent.CATEGORY_DEFAULT); receiver = new ResponseReceiver(); registerReceiver(receiver, filter);}@Overrideprotected void onStop() { unregisterReceiver(receiver); super.onStop();}
![Page 77: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/77.jpg)
Receiver<application... > ... <receiver android:name=".MainActivity$ResponseReceiver"/></application>
![Page 78: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/78.jpg)
BroadcastIntent broadcastIntent = new Intent();
broadcastIntent.setAction(MainActivity.ResponseReceiver.ACTION_RESP);
broadcastIntent.addCategory(Intent.CATEGORY_DEFAULT);
broadcastIntent.putExtra(PROGRESS, ""+i);
sendBroadcast(broadcastIntent);
*sendStickyBroadcast() - no longer available
![Page 79: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/79.jpg)
Security hole!!!Security Threat!!!
![Page 80: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/80.jpg)
How to treat?
Use LocalBroadcastManagerValidate the intent that you gotValidate permissions
![Page 81: Lecture #3 activities and intents](https://reader034.vdocuments.mx/reader034/viewer/2022042908/58ed8dee1a28abe0468b46a7/html5/thumbnails/81.jpg)