1

I'm like in a kind of a vicious circle. I'm trying to implement MVVM pattern into my app, but to instantiate ViewModel the next way:

ViewModel loginViewModel = new ViewModelProvider(this).get(LoginViewModel.class);

it's necessary to extend AppCompat, but when I extend AppCompatActivity in my BaseActivity class I get an exception of type:

"You need to use a Theme.AppCompat theme (or descendant) with this activity"

And the problem is I cannot use AppCompat theme in manifest because I'm already using

android:theme="@style/Theme.myTheme.TitleBar"

TitleBar:

<resources>
  <!--parent="@style/Theme.AppCompat"-->
  <style name="Theme.myTheme.TitleBar" parent="android:Theme">
    <item name="android:windowTitleSize">50dip</item>
  </style>
</resources>

and then I configure the ActionBar in code and then in BaseActivity:

requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);

So I don't know how to combine extending AppCompatActivity with custom title feature.

Edit 1: In reply to @CommonsWare

If I set

android:theme="@style/Theme.AppCompat.NoActionBar"

in my Manifest file then I get thenext exception:

"You cannot combine custom titles with other title features"

as soon as the app starts, in the first line of the next piece of code which creates the drawer menu container:

// HACK: "steal" the first child of decor view
ViewGroup decor = (ViewGroup) ((Activity)context).getWindow().getDecorView();
View child = decor.getChildAt(0);
decor.removeView(child);
FrameLayout container = drawer.findViewById(R.id.container); // This is the container we defined just now.
container.addView(child);

So I guess no luck yet :(

Diego Perez
  • 2,188
  • 2
  • 30
  • 58
  • 1
    What use is the `android:Theme` for you? – gtxtreme Sep 27 '21 at 23:33
  • 1
    Why not use `Theme.AppCompat.NoActionBar` as the base theme? "it's necessary to extend AppCompat" -- to use `ViewModel`, I believe it is only necessary to extend `ComponentActivity`, not `AppCompatActivity` (which itself extends from `ComponentActivity`). – CommonsWare Sep 27 '21 at 23:39
  • Thanks @CommonsWare, I tried your suggestion and edited my question in reply. Please take a look at it. – Diego Perez Sep 28 '21 at 10:41
  • Thanks for your reply @gtxtreme. Not totally sure why I need android:theme, but the fact it it won't compile without setting a parent for the style, and if I set something different from android:theme I have the "You cannot combine custom titles with other title features" error. – Diego Perez Sep 28 '21 at 10:44

0 Answers0