Skip to content

Commit bd25854

Browse files
committed
readme
1 parent 8202d22 commit bd25854

File tree

1 file changed

+9
-82
lines changed

1 file changed

+9
-82
lines changed

doc/screen.md

Lines changed: 9 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
This modules are placed inside the `screen-*` artifacts.
99

10-
# Example - Settings Activity
10+
# Usage with build in settings activity (PREFFERED)
1111

1212
This is an activity with a toolbar and a back button and can be shown as following:
1313

@@ -26,94 +26,21 @@ object ScreenCreator : SettingsActivity.IScreenCreator {
2626
}
2727
```
2828

29-
Alternatively you can simple extend `BaseSettingsActivity` and implement the single abstract `createScreen` function there - whatever you prefer.
29+
This uses a "trick" to provide a small and efficient parcelable setup via an `object` to avoid any problems (either memory nor speed wise) with the parcel size limit and still provides a convenient and simple way to use this library without having to write your own settings activity.
3030

31-
Of course, you can simple place the settings screen inside any layout manually as well, check out the [CustomSettingsActivity](../demo/src/main/java/com/michaelflisar/materialpreferences/demo/activities/CustomSettingsActivity.kt) for an example. This is quite simple as well, simple do following:
31+
# Usage with a custom activity (ALTERNATIVE)
32+
33+
Alternatively you can simple extend `BaseSettingsActivity` and implement the single abstract `createScreen` function there s shown inside the [CustomSettingsActivity](../demo/src/main/java/com/michaelflisar/materialpreferences/demo/activities/CustomSettingsActivity.kt) and with this method you can of course also embed the settings screen inside any bigger layout.
34+
35+
Generally the manual approach works as simple as following:
3236

3337
* create the screen
3438
* bind it to a `RecyclerView`
3539
* forward the back press event to the screen so that it can handle its internal backstack
3640

37-
```kotlin
38-
class CustomSettingsActivity : AppCompatActivity() {
39-
40-
companion object {
41-
fun start(context: Context) {
42-
val intent = Intent(context, CustomSettingsActivity::class.java).apply {
43-
addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
44-
}
45-
context.startActivity(intent)
46-
}
47-
}
48-
49-
lateinit var binding: PreferenceActivitySettingsBinding
50-
lateinit var preferenceScreen: PreferenceScreen
51-
52-
override fun onCreate(savedInstanceState: Bundle?) {
53-
super.onCreate(savedInstanceState)
54-
AppCompatDelegate.setDefaultNightMode(if (DemoSettingsModel.darkTheme.value) AppCompatDelegate.MODE_NIGHT_YES else AppCompatDelegate.MODE_NIGHT_NO)
55-
56-
binding = PreferenceActivitySettingsBinding.inflate(layoutInflater)
57-
val view = binding.root
58-
setContentView(view)
59-
setSupportActionBar(binding.toolbar)
60-
61-
supportActionBar?.setDisplayHomeAsUpEnabled(true)
62-
63-
// ---------------
64-
// set up settings
65-
// ---------------
66-
67-
preferenceScreen = initSettings(savedInstanceState)
68-
}
69-
70-
private fun initSettings(savedInstanceState: Bundle?): PreferenceScreen {
71-
72-
val screen = screen {
73-
74-
// set up screen
75-
state = savedInstanceState
76-
onScreenChanged = { subScreenStack, stateRestored ->
77-
val breadcrumbs =
78-
subScreenStack.joinToString(" > ") { it.title.get(this@CustomSettingsActivity) }
79-
L.d { "Preference Screen - level = ${subScreenStack.size} | $breadcrumbs | restored: $stateRestored" }
80-
supportActionBar?.subtitle = breadcrumbs
81-
}
82-
83-
// -----------------
84-
// create settings screen
85-
// -----------------
86-
87-
// ... create all your settings here ...
88-
// ...
89-
}
90-
91-
// bind the screen to the RecyclerView
92-
screen.bind(binding.rvSettings, this)
93-
94-
return screen
95-
}
96-
97-
override fun onSaveInstanceState(outState: Bundle) {
98-
super.onSaveInstanceState(outState)
99-
preferenceScreen.onSaveInstanceState(outState)
100-
}
101-
102-
override fun onBackPressed() {
103-
if (preferenceScreen.onBackPressed()) {
104-
return
105-
}
106-
super.onBackPressed()
107-
}
41+
Here's an example:
10842

109-
override fun onSupportNavigateUp(): Boolean {
110-
if (!preferenceScreen.onBackPressed()) {
111-
finish()
112-
}
113-
return true
114-
}
115-
}
116-
```
43+
https://github.com/MFlisar/MaterialPreferences/blob/dad0db4565464905da10d4dd17f0ba6a3d92ee0e/demo/src/main/java/com/michaelflisar/materialpreferences/demo/activities/CustomSettingsActivity.kt#L15-105
11744

11845
# Example - Screen
11946

0 commit comments

Comments
 (0)