Lauren Bricker
CSE 340 Spring 2021
Administrivia
There were some constructive criticisms to address from the the midquarter evaluation.
From last time: Which of the following does NOT generate an event?
A. Pressing the CTRL key
B. Pressing the A key
C. Moving your finger on the phone screen
D. Clicking a mouse button (on a computer)
A subtlety this question does not capture:
Each key press is described by a sequence of key events. A key press starts with a key event with ACTION_DOWN. If the key is held sufficiently long that it repeats, then the initial down is followed additional key events with ACTION_DOWN and a non-zero value for getRepeatCount(). The last key event is a ACTION_UP for the key up.
From the Android KeyEvent page
onDraw()
)onLayout()
or XML
)damage
is a bit kept on each view. If state changes, damage
becomes true
If there is damage
do
damage
is a bit kept on each view. If state changes, damage
becomes true
If there is damage
do
onMeasure()
and onLayout()
(if a container)onDraw()
but never call it (call invalidate()
instead)How does the toolkit know what to redraw?
What causes damage?
concrete example on next slide
What should be redrawn?
What should be redrawn?
What should be redrawn?
How does the toolkit know what to redraw?
What causes damage?
How does the toolkit know what to redraw?
What causes damage?
Naive approach to redraw
Naive approach to redraw
TODO ADD pic like this using divs?
Naive approach to redraw - redraw the entire screen
More efficient: calculate only the region necessary
Naive approach to redraw - redraw the entire screen
More efficient: calculate only the region necessary
Never just draw: Why not?
Naive approach to redraw - redraw the entire screen
More efficient: calculate only the region necessary
Never just draw: Why not?
How does the toolkit know what to redraw?
invalidate()
or equivalentHow does the toolkit know what to redraw?
invalidate()
) NOTE we are not calling onDraw() directly (important for your assignment)Virtual device abstraction provided by windowing system
Component abstraction provided by toolkit
Drawing is recursive
Allows each program to (mostly) pretend that it has the screen (frame buffer) to itself
Allows each component to (mostly) pretend that it has the screen to itself
If damage do
onDraw()
)onLayout()
or XML
)And they all want to use tons of memory, but they don't need that memory all the time.
bundle
(In truth the app could write/read its state to disk whenever it is being closed/opened but that is time consuming and would delay the OS launching new things)
bundle
right before your memory is cleared,
and gives you your bundle
back when you get the memory space back.Bundle documentation
activity
layer, not to be confused with any individual view
.onSaveInstanceState(Bundle outState)
on the activity, providing a reference to the activity for the app to save values into.onRestoreInstanceState(Bundle savedInstanceState)
returning the same bundle from earlier.bundle
as a Map<String, Object>
that only supports certain types
of objects (they must be Serializable
which includes all primitives and String
)(Read more here)
public abstract class MainActivity extends AppCompatActivity { @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putString("OUR_KEY", "bundles? bundles!!!"); } @Override protected void onRestoreInstanceState(Bundle savedInstanceState) { super.onRestoreInstanceState(savedInstanceState); String whatWeSaved = savedInstanceState.getString("OUR_KEY"); // whatWeSaved would contain "bundles? bundles!!!" }}
Bundles can be used to comunicate when switching activities.
Example in Ask For Help - in ChooseRequestsActivity
// Instance variable declarationprivate Bundle mMessageInfo;// Instance variable initialization (in setVariables called by onCreate)mMessageInfo = new Bundle();// Setting key/value pairsmMessageInfo.putString("last_button_pressed", "none");// Switching activitiesIntent intent = new Intent(this, ChooseContactGroupsActivity.class);mMessageInfo.remove("last_button_pressed");String request = ((TextView)view).getText().toString();mMessageInfo.putString("last_button_pressed", request);// Attaching the bundle to the intentintent.putExtras(mMessageInfo);startActivity(intent);
Bundles can be used to comunicate when switching activities.
Example in Ask For Help - in ChooseContactsActivity
// Instance variable declarationprivate Bundle mMessageInfo;// Instance variable initialization (in setVariables called by onCreate)mMessageInfo = getIntent().getExtras();// Add things to the bundle in addNumbersToBundle and textPeoplemMessageInfo.putString("contact" + (ii + 1), contactsAsString);// Get information from the bundleString textBody = mMessageInfo.getString("last_button_pressed");String numbers = "smsto:" + mMessageInfo.getString("contact" + contactID);
SharedPreferences documentation
To see what is in your shared preferences
data data <packagename> for your app, such as cse340.askforhelp shared_prefs <packagename>.PREFERENCES.xml
Creating the Shared Preferences File
protected SharedPreferences getPrefs() { if ( mSharedPreferences == null ) { try { Context context = getApplicationContext(); mSharedPreferences = context.getSharedPreferences(context.getPackageName() + ".PREFERENCES", Context.MODE_PRIVATE); } catch (Exception e) { //failed to edit shared preferences file showToast(R.string.shared_pref_error); } } return mSharedPreferences;}
Saving to/restoring from the Shared Preferences file.
// setting dataSharedPreferences.Editor editor = getPrefs().edit();editor.putBoolean("mLocationOn", true);editor.putInt("group_selected", mWhichGroup);editor.putString("contactGroup" + (ii + 1), contactGroup);editor.putStringSet("contact_numbers" + (ii + 1), mNumbers.get(ii));editor.apply(); // have to force the write.// getting data, the last parameter is the default.mLocationOn = getPrefs().getBoolean("mLocationOn", true);mWhichGroup = getPrefs().getInt("group_selected", 1);String contactGroupName = getPrefs().getString("contactGroup" + (ii + 1), "");Set<String> ids = getPrefs().getStringSet("contact_ids" + (ii + 1),
Administrivia
Keyboard shortcuts
↑, ←, Pg Up, k | Go to previous slide |
↓, →, Pg Dn, Space, j | Go to next slide |
Home | Go to first slide |
End | Go to last slide |
Number + Return | Go to specific slide |
b / m / f | Toggle blackout / mirrored / fullscreen mode |
c | Clone slideshow |
p | Toggle presenter mode |
s | Start & Stop the presentation timer |
t | Reset the presentation timer |
?, h | Toggle this help |
Esc | Back to slideshow |