Difference between revisions of "Mobile App Dev 2021W: Tutorial 7"

From Soma-notes
Jump to navigation Jump to search
Line 3: Line 3:
==Tasks==
==Tasks==


# Compare this code with the code from [[Mobile App Development 2021W Lecture 15|Lecture 15]].  What functional differences are there between the apps (other than what messages are logged)?  Be sure to rotate the app fully (a two-click process in the simulator) and use it in both orientations.
<ol>
# What activity lifestyle events happen when you do the following with the app:
<li>Compare this code with the code from [[Mobile App Development 2021W Lecture 15|Lecture 15]].  What functional differences are there between the apps (other than what messages are logged)?  Be sure to rotate the app fully (a two-click process in the simulator) and use it in both orientations.</li>
#* launch the app
<li>What activity lifestyle events happen when you do the following with the app:
#* terminate the app (from the app switcher)
<ol>
#* rotate the device (change device orientation)
<li>launch the app</li>
#* run another app?
<li>terminate the app (from the app switcher)</li>
#* switch back to the app?
<li>rotate the device (change device orientation)</li>
# What code is responsible for saving the app's state?  When is this code called, relative to the main activity's lifecycle?
<li>run another app?</li>
# What code is responsible for restoring the app's state?  When is this code called, relative to the main activity's lifecycle?
<li>switch back to the app?</li>
# How is data organized in a bundle?
</ol></li>
# How is data saved to a bundle?
<li>What code is responsible for saving the app's state?  When is this code called, relative to the main activity's lifecycle?</li>
# How is bundle data loaded?
<li>What code is responsible for restoring the app's state?  When is this code called, relative to the main activity's lifecycle?</li>
# How persistent is bundle-stored data?  How can this be verified?
</li>How is data organized in a bundle?</li>
<li>How is data saved to a bundle?</li>
<li>How is bundle data loaded?</li>
<li>How persistent is bundle-stored data?  How can this be verified?</li>


==Code==
==Code==

Revision as of 20:36, 15 March 2021

This tutorial is due on March 23, 2021 via cuLearn. Please use this template for your solutions.

Tasks

  1. Compare this code with the code from Lecture 15. What functional differences are there between the apps (other than what messages are logged)? Be sure to rotate the app fully (a two-click process in the simulator) and use it in both orientations.
  2. What activity lifestyle events happen when you do the following with the app:
    1. launch the app
    2. terminate the app (from the app switcher)
    3. rotate the device (change device orientation)
    4. run another app?
    5. switch back to the app?
  3. What code is responsible for saving the app's state? When is this code called, relative to the main activity's lifecycle?
  4. What code is responsible for restoring the app's state? When is this code called, relative to the main activity's lifecycle?
  5. How is data organized in a bundle?
  6. How is data saved to a bundle?
  7. How is bundle data loaded?
  8. How persistent is bundle-stored data? How can this be verified?
  9. Code

    TapDemo.zip

    MainActivity.kt

    package carleton.comp1601.tapdemo
    
    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.os.PersistableBundle
    import android.util.Log
    import android.view.View
    import android.widget.TextView
    
    class MainActivity : AppCompatActivity() {
        private lateinit var myMessage: TextView
        private var count = 0
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            Log.d("TapDemo", "Created")
    
            myMessage = findViewById(R.id.myMessage)
    
            if (savedInstanceState != null) {
                with(savedInstanceState) {
                    count = getInt("count")
                }
            } else {
                count = 0
            }
        }
    
        fun myButtonPressed(v: View) {
            count++
            myMessage.text = "You clicked $count times."
    
            Log.d("TapDemo", "Button Pressed")
        }
    
        override fun onSaveInstanceState(outState: Bundle) {
            outState.run {
                putInt("count", count)
            }
            super.onSaveInstanceState(outState)
    
            Log.d("TapDemo", "State saved")
        }
    
        override fun onDestroy() {
            super.onDestroy()
            Log.d("TapDemo", "Destroyed")
        }
    
        override fun onResume() {
            super.onResume()
            Log.d("TapDemo", "Resumed")
        }
    
        override fun onPause() {
            super.onPause()
            Log.d("TapDemo", "Paused")
        }
    
        override fun onStart() {
            super.onStart()
            Log.d("TapDemo", "Started")
        }
    
        override fun onStop() {
            super.onStop()
            Log.d("TapDemo", "Stopped")
        }
    
        override fun onRestart() {
            super.onRestart()
            Log.d("TapDemo", "Restarted")
        }
    }
    

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/TapDemo"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <Button
            android:id="@+id/myButton"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="myButtonPressed"
            android:text="Tap me!"
            android:textAppearance="@style/TextAppearance.AppCompat.Display1"
            app:layout_constraintBottom_toTopOf="@+id/myMessage"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <TextView
            android:id="@+id/myMessage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="You haven't tapped yet."
            android:textAppearance="@style/TextAppearance.AppCompat.Display1"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    </androidx.constraintlayout.widget.ConstraintLayout>