diff --git a/app/build.gradle b/app/build.gradle index 46c1be6..816d1cd 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -36,5 +36,8 @@ android { dependencies { debugImplementation project(":savestateobserver") + implementation 'com.google.android.material:material:1.5.0' + implementation 'androidx.fragment:fragment-ktx:1.4.1' implementation 'androidx.appcompat:appcompat:1.4.1' + implementation 'androidx.startup:startup-runtime:1.1.1' } diff --git a/app/src/debug/kotlin/com/pnuema/android/savestateobserver/app/AppInitializer.kt b/app/src/debug/kotlin/com/pnuema/android/savestateobserver/app/AppInitializer.kt new file mode 100644 index 0000000..92b635e --- /dev/null +++ b/app/src/debug/kotlin/com/pnuema/android/savestateobserver/app/AppInitializer.kt @@ -0,0 +1,20 @@ +package com.pnuema.android.savestateobserver.app + +import android.content.Context +import android.util.Log +import androidx.startup.Initializer +import com.pnuema.android.savestateobserver.OversizeBundleRegistrar + +class AppInitializer: Initializer { + override fun create(context: Context) { + OversizeBundleRegistrar.register { stringifyBundle -> + Log.e( + "AppBundleWorker", + "OVERSIZE BUNDLE DETECTED: $stringifyBundle" + ) + } + } + + override fun dependencies(): MutableList>> = mutableListOf() + +} \ No newline at end of file diff --git a/app/src/debug/kotlin/com/pnuema/android/savestateobserver/app/BaseActivity.kt b/app/src/debug/kotlin/com/pnuema/android/savestateobserver/app/BaseActivity.kt deleted file mode 100644 index 6749bb2..0000000 --- a/app/src/debug/kotlin/com/pnuema/android/savestateobserver/app/BaseActivity.kt +++ /dev/null @@ -1,31 +0,0 @@ -package com.pnuema.android.savestateobserver.app - -import android.os.Bundle -import android.util.Log -import androidx.appcompat.app.AppCompatActivity -import com.pnuema.android.savestateobserver.OversizeBundleRegistrar -import java.util.* - -open class BaseActivity: AppCompatActivity() { - - private lateinit var uuid: UUID - - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.activity_main) - - uuid = OversizeBundleRegistrar.register { stringifyBundle -> - Log.e( - "AppBundleWorker", - "OVERSIZE BUNDLE DETECTED: $stringifyBundle" - ) - } - } - - override fun onDestroy() { - if (::uuid.isInitialized) { - OversizeBundleRegistrar.unregister(uuid) - } - super.onDestroy() - } -} \ No newline at end of file diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 3f3e34c..d221dbc 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -1,26 +1,34 @@ - - - - + - + + + + \ No newline at end of file diff --git a/app/src/main/kotlin/com/pnuema/android/savestateobserver/app/BundleGenerator.kt b/app/src/main/kotlin/com/pnuema/android/savestateobserver/app/BundleGenerator.kt new file mode 100644 index 0000000..09c9655 --- /dev/null +++ b/app/src/main/kotlin/com/pnuema/android/savestateobserver/app/BundleGenerator.kt @@ -0,0 +1,29 @@ +package com.pnuema.android.savestateobserver.app + +import android.os.Bundle +import java.util.* + +object BundleGenerator { + /** + * Generate oversize bundle + */ + fun Bundle.generateOversizeBundle() = apply { + putInt("Integer", 1234) + putString("String", "StringTest") + putFloat("Float", 12.34F) + + val innerBundle = Bundle() + innerBundle.putInt("Integer", 5678) + innerBundle.putString("String", "InnerStringTest") + innerBundle.putFloat("Float", 56.78F) + + //generate 50k of data for the bundle + var bigString = "" + while (bigString.length < 50000) { + bigString += UUID.randomUUID().toString() + } + innerBundle.putString("BigString", bigString) + + putBundle("innerBundle", innerBundle) + } +} \ No newline at end of file diff --git a/app/src/main/kotlin/com/pnuema/android/savestateobserver/app/MainActivity.kt b/app/src/main/kotlin/com/pnuema/android/savestateobserver/app/MainActivity.kt index 252e281..10777ea 100644 --- a/app/src/main/kotlin/com/pnuema/android/savestateobserver/app/MainActivity.kt +++ b/app/src/main/kotlin/com/pnuema/android/savestateobserver/app/MainActivity.kt @@ -1,33 +1,21 @@ package com.pnuema.android.savestateobserver.app import android.os.Bundle -import java.util.* +import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.commit +import com.pnuema.android.savestateobserver.app.BundleGenerator.generateOversizeBundle -class MainActivity : BaseActivity() { - override fun onSaveInstanceState(outState: Bundle) { - outState.putInt("Integer", 1234) - outState.putString("String", "StringTest") - outState.putFloat("Float", 12.34F) - - val innerBundle = Bundle() - innerBundle.putInt("Integer", 5678) - innerBundle.putString("String", "InnerStringTest") - innerBundle.putFloat("Float", 56.78F) +class MainActivity : AppCompatActivity(R.layout.activity_main) { + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) - //generate 50k of data for the bundle - var bigString = "" - while (bigString.length < 50000) { - bigString += UUID.randomUUID().toString() + supportFragmentManager.commit { + replace(R.id.fragment_container_view, MainFragment.newInstance()) } - innerBundle.putString("BigString", bigString) - - outState.putBundle("innerBundle", innerBundle) - - super.onSaveInstanceState(outState) } - override fun onCreate(savedInstanceState: Bundle?) { - super.onCreate(savedInstanceState) - setContentView(R.layout.activity_main) + override fun onSaveInstanceState(outState: Bundle) { + outState.generateOversizeBundle() + super.onSaveInstanceState(outState) } } diff --git a/app/src/main/kotlin/com/pnuema/android/savestateobserver/app/MainFragment.kt b/app/src/main/kotlin/com/pnuema/android/savestateobserver/app/MainFragment.kt new file mode 100644 index 0000000..f763820 --- /dev/null +++ b/app/src/main/kotlin/com/pnuema/android/savestateobserver/app/MainFragment.kt @@ -0,0 +1,23 @@ +package com.pnuema.android.savestateobserver.app + +import android.os.Bundle +import androidx.fragment.app.Fragment +import com.pnuema.android.savestateobserver.app.BundleGenerator.generateOversizeBundle + +/** + * A simple [Fragment] subclass. + * Use the [MainFragment.newInstance] factory method to + * create an instance of this fragment. + */ +class MainFragment private constructor() : Fragment(R.layout.fragment_main) { + companion object { + fun newInstance() = MainFragment().apply { + arguments = Bundle().generateOversizeBundle() //TODO detect oversize arguments + } + } + + override fun onSaveInstanceState(outState: Bundle) { + outState.generateOversizeBundle() + super.onSaveInstanceState(outState) + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index e9701b0..0aad31a 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,15 +1,9 @@ - - - - - \ No newline at end of file + tools:context=".MainActivity" /> \ No newline at end of file diff --git a/app/src/main/res/layout/fragment_main.xml b/app/src/main/res/layout/fragment_main.xml new file mode 100644 index 0000000..d479b7b --- /dev/null +++ b/app/src/main/res/layout/fragment_main.xml @@ -0,0 +1,17 @@ + + + + + + \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d205097..743518e 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,4 +1,4 @@ SaveStateObserver - Minimize this app while watching the LogCat to see the results + Minimize this app while watching LogCat to see the results diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 5885930..3d8448c 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,7 +1,7 @@ -