@@ -5,20 +5,27 @@ import android.widget.Toast
55import androidx.activity.compose.setContent
66import androidx.activity.enableEdgeToEdge
77import androidx.appcompat.app.AppCompatActivity
8+ import androidx.compose.foundation.background
9+ import androidx.compose.foundation.layout.Arrangement
810import androidx.compose.foundation.layout.Column
911import androidx.compose.foundation.layout.fillMaxSize
1012import androidx.compose.foundation.layout.fillMaxWidth
1113import androidx.compose.foundation.layout.padding
14+ import androidx.compose.foundation.shape.RoundedCornerShape
1215import androidx.compose.material3.Button
16+ import androidx.compose.material3.Card
17+ import androidx.compose.material3.CardDefaults
18+ import androidx.compose.material3.MaterialTheme
1319import androidx.compose.material3.Scaffold
1420import androidx.compose.material3.Text
1521import androidx.compose.runtime.Composable
1622import androidx.compose.runtime.getValue
1723import androidx.compose.runtime.mutableStateOf
18- import androidx.compose.runtime.remember
24+ import androidx.compose.runtime.saveable.rememberSaveable
1925import androidx.compose.runtime.setValue
2026import androidx.compose.ui.Alignment
2127import androidx.compose.ui.Modifier
28+ import androidx.compose.ui.draw.clip
2229import androidx.compose.ui.text.style.TextAlign
2330import androidx.compose.ui.tooling.preview.Preview
2431import androidx.compose.ui.unit.dp
@@ -30,70 +37,114 @@ import com.rnapp.brownfieldlib.ReactNativeHostManager
3037
3138class MainActivity : AppCompatActivity () {
3239 override fun onCreate (savedInstanceState : Bundle ? ) {
33- super .onCreate(savedInstanceState )
40+ super .onCreate(null )
3441 enableEdgeToEdge()
3542
36- ReactNativeHostManager .initialize(this .application) {
37- Toast .makeText(this , " React Native has been loaded" , Toast .LENGTH_LONG ).show()
43+ if (savedInstanceState == null ) {
44+ ReactNativeHostManager .initialize(application) {
45+ Toast .makeText(
46+ this ,
47+ " React Native has been loaded" ,
48+ Toast .LENGTH_LONG
49+ ).show()
50+ }
3851 }
3952
4053 setContent {
4154 AndroidBrownfieldAppTheme {
42- Scaffold (modifier = Modifier .fillMaxSize()) { innerPadding ->
43- Column (modifier = Modifier .padding(innerPadding)) {
44- Greeting (
45- name = " Android" ,
46- modifier = Modifier
47- .fillMaxWidth()
48- .padding(innerPadding)
49- .padding(vertical = 1 .dp),
50- )
51-
52- ReactNativeView ()
53- }
55+ Scaffold (
56+ modifier = Modifier .fillMaxSize()
57+ ) { innerPadding ->
58+ MainScreen (
59+ modifier = Modifier
60+ .fillMaxSize()
61+ .padding(innerPadding)
62+ .padding(16 .dp) // outer margin
63+ )
5464 }
5565 }
5666 }
5767 }
5868}
5969
6070@Composable
61- fun Greeting (name : String , modifier : Modifier = Modifier ) {
62- var counter by remember { mutableStateOf(0 ) }
63-
71+ private fun MainScreen (modifier : Modifier = Modifier ) {
6472 Column (
6573 modifier = modifier,
66- horizontalAlignment = Alignment .CenterHorizontally
74+ verticalArrangement = Arrangement .spacedBy(16 .dp),
75+ horizontalAlignment = Alignment .CenterHorizontally // center top bar content
6776 ) {
68- Text (
69- text = " Hello $name , you clicked the button $counter time${if (counter == 1 ) " " else " s" } !" ,
70- textAlign = TextAlign .Center ,
77+ GreetingCard (
78+ name = " Android" ,
79+ modifier = Modifier .fillMaxWidth()
80+ )
81+
82+ ReactNativeView (
7183 modifier = Modifier
7284 .fillMaxWidth()
85+ .clip(RoundedCornerShape (16 .dp))
86+ .background(MaterialTheme .colorScheme.surface)
7387 )
88+ }
89+ }
90+
91+ @Composable
92+ fun GreetingCard (
93+ name : String ,
94+ modifier : Modifier = Modifier
95+ ) {
96+ var counter by rememberSaveable { mutableStateOf(0 ) }
7497
75- Button (
76- onClick = { counter++ },
98+ Card (
99+ modifier = modifier,
100+ shape = RoundedCornerShape (16 .dp),
101+ elevation = CardDefaults .cardElevation(defaultElevation = 4 .dp)
102+ ) {
103+ Column (
104+ modifier = Modifier
105+ .padding(16 .dp)
106+ .fillMaxWidth(),
107+ horizontalAlignment = Alignment .CenterHorizontally ,
108+ verticalArrangement = Arrangement .spacedBy(12 .dp)
77109 ) {
78- Text (text = " Increment counter" )
110+ Text (
111+ text = " Hello native $name 👋" ,
112+ style = MaterialTheme .typography.titleMedium,
113+ textAlign = TextAlign .Center
114+ )
115+
116+ Text (
117+ text = " You clicked the button $counter time${if (counter == 1 ) " " else " s" } " ,
118+ textAlign = TextAlign .Center ,
119+ style = MaterialTheme .typography.bodyMedium
120+ )
121+
122+ Button (onClick = { counter++ }) {
123+ Text (" Increment counter" )
124+ }
79125 }
80126 }
81127}
82128
83129@Composable
84- fun ReactNativeView () {
130+ fun ReactNativeView (
131+ modifier : Modifier = Modifier
132+ ) {
85133 AndroidFragment <ReactNativeFragment >(
134+ modifier = modifier,
86135 arguments = Bundle ().apply {
87- putString(ReactNativeFragmentArgNames .ARG_MODULE_NAME , " RNApp" )
88- }, modifier = Modifier
89- .fillMaxSize()
136+ putString(
137+ ReactNativeFragmentArgNames .ARG_MODULE_NAME ,
138+ " RNApp"
139+ )
140+ }
90141 )
91142}
92143
93144@Preview(showBackground = true )
94145@Composable
95146fun GreetingPreview () {
96147 AndroidBrownfieldAppTheme {
97- Greeting ( " Android " )
148+ MainScreen ( )
98149 }
99150}
0 commit comments