How can I use the toolbar without the AppCompatActivity extension

I have an activity HomeViewthat is already expanding another activity and cannot expand AppCompatActivity. But HomeView should have a toolbar. The Android documentation says that any activity that should have a toolbar should expand AppCompatActivity.

How can I get around this limitation?

+3
source share
1 answer

You need to implement AppCompatCallbackand use AppCompatDelegate. Here is a great article on how to use it: https://medium.com/google-developer-experts/how-to-add-toolbar-to-an-activity-which-doesn-t-extend-appcompatactivity-a07c026717b3# .nuyghrgr9 , and also check https://developer.android.com/reference/android/support/v7/app/AppCompatDelegate.html to find out which methods to delegate.


AppCompatDelegate

This class represents a delegate that you can use to extend AppCompat support for any Activity.

When using AppCompatDelegate you should use some methods, not an Activity method with the same name. It refers to:

addContentView(android.view.View, android.view.ViewGroup.LayoutParams)
setContentView(int)
setContentView(android.view.View)
setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
requestWindowFeature(int)
invalidateOptionsMenu()
startSupportActionMode(android.support.v7.view.ActionMode.Callback)
setSupportActionBar(android.support.v7.widget.Toolbar)
getSupportActionBar()
getMenuInflater()

There are also some Activity lifecycle methods that need to be proxied for the delegate:

onCreate(android.os.Bundle)
onPostCreate(android.os.Bundle)
onConfigurationChanged(android.content.res.Configuration)
setTitle(CharSequence)
onStop()
onDestroy()
+3

Source: https://habr.com/ru/post/1613767/


All Articles