Skip to content

Commit e2f5080

Browse files
committed
Fix fragment observation, replace contentprovider with initializer, modernization
1 parent 20fbb3d commit e2f5080

26 files changed

Lines changed: 174 additions & 129 deletions

File tree

app/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,8 @@ android {
3636

3737
dependencies {
3838
debugImplementation project(":savestateobserver")
39+
implementation 'com.google.android.material:material:1.5.0'
40+
implementation 'androidx.fragment:fragment-ktx:1.4.1'
3941
implementation 'androidx.appcompat:appcompat:1.4.1'
42+
implementation 'androidx.startup:startup-runtime:1.1.1'
4043
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.pnuema.android.savestateobserver.app
2+
3+
import android.content.Context
4+
import android.util.Log
5+
import androidx.startup.Initializer
6+
import com.pnuema.android.savestateobserver.OversizeBundleRegistrar
7+
8+
class AppInitializer: Initializer<Unit> {
9+
override fun create(context: Context) {
10+
OversizeBundleRegistrar.register { stringifyBundle ->
11+
Log.e(
12+
"AppBundleWorker",
13+
"OVERSIZE BUNDLE DETECTED: $stringifyBundle"
14+
)
15+
}
16+
}
17+
18+
override fun dependencies(): MutableList<Class<out Initializer<*>>> = mutableListOf()
19+
20+
}

app/src/debug/kotlin/com/pnuema/android/savestateobserver/app/BaseActivity.kt

Lines changed: 0 additions & 31 deletions
This file was deleted.

app/src/main/AndroidManifest.xml

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,34 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<manifest
3-
xmlns:android="http://schemas.android.com/apk/res/android"
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
xmlns:tools="http://schemas.android.com/tools"
44
package="com.pnuema.android.savestateobserver.app">
55

66
<application
7-
android:fullBackupContent="true"
87
android:allowBackup="true"
98
android:icon="@mipmap/ic_launcher"
109
android:label="@string/app_name"
1110
android:roundIcon="@mipmap/ic_launcher_round"
1211
android:supportsRtl="true"
1312
android:theme="@style/AppTheme">
14-
15-
<activity android:name=".MainActivity"
13+
<activity
14+
android:name=".MainActivity"
1615
android:exported="true">
1716
<intent-filter>
18-
<action android:name="android.intent.action.MAIN"/>
17+
<action android:name="android.intent.action.MAIN" />
1918

20-
<category android:name="android.intent.category.LAUNCHER"/>
19+
<category android:name="android.intent.category.LAUNCHER" />
2120
</intent-filter>
2221
</activity>
2322

23+
<provider
24+
android:name="androidx.startup.InitializationProvider"
25+
android:authorities="${applicationId}.androidx-startup"
26+
android:exported="false"
27+
tools:node="merge">
28+
<meta-data
29+
android:name="com.pnuema.android.savestateobserver.app.AppInitializer"
30+
android:value="androidx.startup" />
31+
</provider>
2432
</application>
2533

2634
</manifest>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.pnuema.android.savestateobserver.app
2+
3+
import android.os.Bundle
4+
import java.util.*
5+
6+
object BundleGenerator {
7+
/**
8+
* Generate oversize bundle
9+
*/
10+
fun Bundle.generateOversizeBundle() = apply {
11+
putInt("Integer", 1234)
12+
putString("String", "StringTest")
13+
putFloat("Float", 12.34F)
14+
15+
val innerBundle = Bundle()
16+
innerBundle.putInt("Integer", 5678)
17+
innerBundle.putString("String", "InnerStringTest")
18+
innerBundle.putFloat("Float", 56.78F)
19+
20+
//generate 50k of data for the bundle
21+
var bigString = ""
22+
while (bigString.length < 50000) {
23+
bigString += UUID.randomUUID().toString()
24+
}
25+
innerBundle.putString("BigString", bigString)
26+
27+
putBundle("innerBundle", innerBundle)
28+
}
29+
}
Lines changed: 11 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,21 @@
11
package com.pnuema.android.savestateobserver.app
22

33
import android.os.Bundle
4-
import java.util.*
4+
import androidx.appcompat.app.AppCompatActivity
5+
import androidx.fragment.app.commit
6+
import com.pnuema.android.savestateobserver.app.BundleGenerator.generateOversizeBundle
57

6-
class MainActivity : BaseActivity() {
7-
override fun onSaveInstanceState(outState: Bundle) {
8-
outState.putInt("Integer", 1234)
9-
outState.putString("String", "StringTest")
10-
outState.putFloat("Float", 12.34F)
11-
12-
val innerBundle = Bundle()
13-
innerBundle.putInt("Integer", 5678)
14-
innerBundle.putString("String", "InnerStringTest")
15-
innerBundle.putFloat("Float", 56.78F)
8+
class MainActivity : AppCompatActivity(R.layout.activity_main) {
9+
override fun onCreate(savedInstanceState: Bundle?) {
10+
super.onCreate(savedInstanceState)
1611

17-
//generate 50k of data for the bundle
18-
var bigString = ""
19-
while (bigString.length < 50000) {
20-
bigString += UUID.randomUUID().toString()
12+
supportFragmentManager.commit {
13+
replace(R.id.fragment_container_view, MainFragment.newInstance())
2114
}
22-
innerBundle.putString("BigString", bigString)
23-
24-
outState.putBundle("innerBundle", innerBundle)
25-
26-
super.onSaveInstanceState(outState)
2715
}
2816

29-
override fun onCreate(savedInstanceState: Bundle?) {
30-
super.onCreate(savedInstanceState)
31-
setContentView(R.layout.activity_main)
17+
override fun onSaveInstanceState(outState: Bundle) {
18+
outState.generateOversizeBundle()
19+
super.onSaveInstanceState(outState)
3220
}
3321
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.pnuema.android.savestateobserver.app
2+
3+
import android.os.Bundle
4+
import androidx.fragment.app.Fragment
5+
import com.pnuema.android.savestateobserver.app.BundleGenerator.generateOversizeBundle
6+
7+
/**
8+
* A simple [Fragment] subclass.
9+
* Use the [MainFragment.newInstance] factory method to
10+
* create an instance of this fragment.
11+
*/
12+
class MainFragment private constructor() : Fragment(R.layout.fragment_main) {
13+
companion object {
14+
fun newInstance() = MainFragment().apply {
15+
arguments = Bundle().generateOversizeBundle() //TODO detect oversize arguments
16+
}
17+
}
18+
19+
override fun onSaveInstanceState(outState: Bundle) {
20+
outState.generateOversizeBundle()
21+
super.onSaveInstanceState(outState)
22+
}
23+
}
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,9 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<LinearLayout
2+
<FrameLayout
33
xmlns:android="http://schemas.android.com/apk/res/android"
44
xmlns:tools="http://schemas.android.com/tools"
5+
android:id="@+id/fragment_container_view"
56
android:layout_width="match_parent"
67
android:layout_height="match_parent"
78
android:gravity="center"
8-
tools:context=".MainActivity">
9-
10-
<TextView
11-
android:layout_width="wrap_content"
12-
android:layout_height="wrap_content"
13-
android:text="@string/instructions"/>
14-
15-
</LinearLayout>
9+
tools:context=".MainActivity" />
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout
3+
xmlns:android="http://schemas.android.com/apk/res/android"
4+
xmlns:tools="http://schemas.android.com/tools"
5+
android:layout_width="match_parent"
6+
android:layout_height="match_parent"
7+
tools:context=".MainFragment">
8+
9+
<TextView
10+
android:layout_width="wrap_content"
11+
android:layout_height="wrap_content"
12+
android:text="@string/instructions"
13+
android:textSize="24sp"
14+
android:textAlignment="center"
15+
android:layout_gravity="center"/>
16+
17+
</FrameLayout>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
<resources>
22
<string name="app_name">SaveStateObserver</string>
3-
<string name="instructions">Minimize this app while watching the LogCat to see the results</string>
3+
<string name="instructions">Minimize this app while watching LogCat to see the results</string>
44
</resources>

0 commit comments

Comments
 (0)