Zhuinden / simple-stack

[ACTIVE] Simple Stack, a backstack library / navigation framework for simpler navigation and state management (for fragments, views, or whatevers).
Apache License 2.0
1.36k stars 75 forks source link

Can I Use @styles/Theme.MyComposeTheme on AndroidManifests.xml? #265

Closed ydhnwb closed 1 year ago

ydhnwb commented 1 year ago

I'm learning to implement SimpleStack into Compose (material3) project. Navigation seems to be works perfectly and easy to understand, but the only problem is, when I use default theme that comes when creates Compose (material3) project, it force-close and shows me error like this:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ydhnwb.learnjetpacknav/com.ydhnwb.learnjetpacknav.app.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

Here is my manifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:name=".app.CustomApplication"
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.LearnJetpackNav"
        tools:targetApi="31">
        <activity
            android:name=".app.MainActivity"
            android:exported="true"
            android:label="@string/app_name"
            android:theme="@style/Theme.LearnJetpackNav">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

            <meta-data
                android:name="android.app.lib_name"
                android:value="" />
        </activity>
    </application>

</manifest>

So, I tried to create a new theme on themes.xml like below just to make it work

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#000</item>
        <item name="colorPrimaryDark">#000</item>
        <item name="colorAccent">#000</item>
    </style>

<!--    can i use below style? -->
    <style name="Theme.LearnJetpackNav" parent="android:Theme.Material.Light.NoActionBar" />
</resources>

then on manifest, I replaced the @styles/Theme.LearnJetpackNavTheme into @styles/AppTheme

It works, but is it okay? Is it affecting app theme like dynamic color (for android12+) or not? I just worried about that. I also wrap my app view like this:

image

Here is my repo (just in case) *all my code is based on this extension-compose-example

Zhuinden commented 1 year ago

Simple-Stack out of the box has no dependency on AndroidX nor AppCompat at all, but Fragments and Material Components that you use, might. In that case, the theme you define should inherit from a parent theme that is requested by your views (both AppCompat alone, and Material-Components has its own themes that they expect to be used as a parent).

ydhnwb commented 1 year ago

Yes. Since I use extends MainActivity with AppCompatActivity to get the "supportFragmentManager", I need to use AppCompat theme.

The default when creating Material3 Compose is extending MainActivity with ComponentActivity, but using it make me unable to get supportFragmentManager

image

image

Update: I found the solution

Zhuinden commented 1 year ago

To use fragments, you might need either FragmentActivity or AppCompatActivity

FragmentActivity should also be a subclass of ComponentActivity