Swipe your finger to draw a picture. Change the orientation of the device (porttrait to landscape) to erase the picture and start again.
MainActivity.java
:
creates an
EtchView
object.
EtchView.java
:
build up a longer and longer
Path
.
We saw a
Path
in the triangle example in
Japan.
activity_main.xml
:
unused and ignored.
AndroidManifest.xml
:
untouched.
build.gradle
(Module: app)
app/java/com.example.android.apis/graphics/FingerPaint.java
:
class
FingerPaint
is a subclass of class
GraphicsActivity
.
app/java/com.example.android.apis/graphics/GraphicsActivity.java
:
class
GraphicsActivity
is a subclass of class
Activity
.
It adds very little to class
Activity
.
app/java/com.example.android.apis/graphics/TouchPaint.java
:
class
TouchPaint
is a subclass of class
GraphicsActivity
.
app/java/com.example.android.apis/graphics/GraphicsActivity.java
:
class
GraphicsActivity
is a subclass of class
Activity
.
It adds very little to class
Activity
.
onDraw
is called so often,
we should make it as fast as possible.
Remove its call to
drawColor
and replace it by the following statement in the constructor of
EtchView
immediately after
super(context);
.
setBackgroundColor(Color.WHITE);
MainActivity
all three pairs of
lifecyle
methods.
Put a call to
Log.d
onCreate
,
too,
and import
android.util.Log
.@Override protected void onStart() { super.onStart(); Log.d("myTag", "onStart"); } @Override protected void onResume() { super.onResume(); Log.d("myTag", "onResume"); } @Override protected void onPause() { super.onPause(); Log.d("myTag", "onPause"); } @Override protected void onStop() { super.onStop(); Log.d("myTag", "onStop"); } @Override protected void onDestroy() { super.onDestroy(); Log.d("myTag", "onDestroy"); }Watch the output in your Mac Terminal or PC Command Prompt window as we did in Text.
adb logcat myTag:D '*:S'Launch the app with the emulator in portrait orientation and then switch to landscape.
D/myTag ( 2018): onPause (launched in portrait orientation) D/myTag ( 2018): onStart D/myTag ( 2018): onResume D/myTag ( 2018): onPause (switch to landscape) D/myTag ( 2018): onStop D/myTag ( 2018): onDestroy D/myTag ( 2018): onPause D/myTag ( 2018): onStart D/myTag ( 2018): onResume D/myTag ( 2018): onPause (switch back to portrait) D/myTag ( 2018): onStop D/myTag ( 2018): onDestroy D/myTag ( 2018): onPause D/myTag ( 2018): onStart D/myTag ( 2018): onResume
To prevent the
Activity
object from being destroyed and recreated,
see
Configuration
Changes.
In the
AndroidManifest.xml
file,
<activity android:name=".MainActivity" android:label="@string/app_name" android:configChanges="orientation|screenSize" >
@Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Toast.makeText(this, "onConfigurationChanged", Toast.LENGTH_LONG).show(); //etc. }
To test it, draw a line in the upper left corner of the
EtchView
.
.xml
file or programmatically.
In the latter case,
don’t forget the
addView
we saw in
Programmatically.