public abstract class AppCompatDelegate extends Object
Activity
.
When using an AppCompatDelegate
, you should call the following methods instead of the
Activity
method of the same name:
addContentView(android.view.View, android.view.ViewGroup.LayoutParams)
setContentView(int)
setContentView(android.view.View)
setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
requestWindowFeature(int)
hasWindowFeature(int)
invalidateOptionsMenu()
startSupportActionMode(android.support.v7.view.ActionMode.Callback)
setSupportActionBar(android.support.v7.widget.Toolbar)
getSupportActionBar()
getMenuInflater()
findViewById(int)
The following methods should be called from the Activity
method of the
same name:
onCreate(android.os.Bundle)
onPostCreate(android.os.Bundle)
onConfigurationChanged(android.content.res.Configuration)
onStart()
onStop()
onPostResume()
onSaveInstanceState(Bundle)
setTitle(CharSequence)
onStop()
onDestroy()
An Activity
can only be linked with one AppCompatDelegate
instance,
therefore the instance returned from create(Activity, AppCompatCallback)
should be
retained until the Activity is destroyed.
Modifier and Type | Class and Description |
---|---|
static interface |
AppCompatDelegate.NightMode |
Modifier and Type | Field and Description |
---|---|
static int |
FEATURE_ACTION_MODE_OVERLAY
Flag for specifying the behavior of action modes when an Action Bar is not present.
|
static int |
FEATURE_SUPPORT_ACTION_BAR
Flag for enabling the support Action Bar.
|
static int |
FEATURE_SUPPORT_ACTION_BAR_OVERLAY
Flag for requesting an support Action Bar that overlays window content.
|
static int |
MODE_NIGHT_AUTO
Mode which means to use night mode when it is determined that it is night or not.
|
static int |
MODE_NIGHT_FOLLOW_SYSTEM
Mode which uses the system's night mode setting to determine if it is night or not.
|
static int |
MODE_NIGHT_NO
Mode which means to not use night mode, and therefore prefer
notnight qualified
resources where available, regardless of the time. |
static int |
MODE_NIGHT_YES
Mode which means to always use night mode, and therefore prefer
night qualified
resources where available, regardless of the time. |
Modifier and Type | Method and Description |
---|---|
abstract void |
addContentView(View v,
ViewGroup.LayoutParams lp)
Should be called instead of
Activity.addContentView(android.view.View, android.view.ViewGroup.LayoutParams) } |
abstract boolean |
applyDayNight()
Allow AppCompat to apply the
night and notnight resource qualifiers. |
static AppCompatDelegate |
create(Activity activity,
AppCompatCallback callback)
Create a
AppCompatDelegate to use with activity . |
static AppCompatDelegate |
create(Dialog dialog,
AppCompatCallback callback)
Create a
AppCompatDelegate to use with dialog . |
abstract View |
createView(View parent,
String name,
Context context,
AttributeSet attrs)
This should be called from a
LayoutInflaterFactory in order
to return tint-aware widgets. |
abstract View |
findViewById(int id)
Finds a view that was identified by the id attribute from the XML that
was processed in
onCreate(android.os.Bundle) . |
static int |
getDefaultNightMode()
Returns the default night mode.
|
abstract ActionBarDrawerToggle.Delegate |
getDrawerToggleDelegate()
Returns an
ActionBarDrawerToggle.Delegate which can be returned from your Activity
if it implements ActionBarDrawerToggle.DelegateProvider . |
abstract MenuInflater |
getMenuInflater()
Return the value of this call from your
Activity.getMenuInflater() |
abstract ActionBar |
getSupportActionBar()
Support library version of
Activity.getActionBar() . |
abstract boolean |
hasWindowFeature(int featureId)
Query for the availability of a certain feature.
|
abstract void |
installViewFactory()
Installs AppCompat's
LayoutInflater Factory so that it can replace
the framework widgets with compatible tinted versions. |
abstract void |
invalidateOptionsMenu()
Should be called from
Activity.invalidateOptionsMenu() } or
FragmentActivity.supportInvalidateOptionsMenu() . |
static boolean |
isCompatVectorFromResourcesEnabled()
Returns whether vector drawables on older platforms (< API 21) can be accessed from within
resources.
|
abstract boolean |
isHandleNativeActionModesEnabled()
Returns whether AppCompat handles any native action modes itself.
|
abstract void |
onConfigurationChanged(Configuration newConfig)
Should be called from
Activity.onConfigurationChanged(android.content.res.Configuration) |
abstract void |
onCreate(Bundle savedInstanceState)
Should be called from
Activity.onCreate() . |
abstract void |
onDestroy()
Should be called from
Activity.onDestroy() |
abstract void |
onPostCreate(Bundle savedInstanceState)
Should be called from
Activity.onPostCreate(android.os.Bundle) |
abstract void |
onPostResume()
Should be called from
Activity.onPostResume() |
abstract void |
onSaveInstanceState(Bundle outState)
Allows AppCompat to save instance state.
|
abstract void |
onStart()
Should be called from
Activity.onStart() Activity.onStart()} |
abstract void |
onStop()
Should be called from
Activity.onStop() |
abstract boolean |
requestWindowFeature(int featureId)
Enable extended window features.
|
static void |
setCompatVectorFromResourcesEnabled(boolean enabled)
Sets whether vector drawables on older platforms (< API 21) can be used within
DrawableContainer resources. |
abstract void |
setContentView(int resId)
Should be called instead of
Activity.setContentView(int) } |
abstract void |
setContentView(View v)
Should be called instead of
Activity.setContentView(android.view.View) } |
abstract void |
setContentView(View v,
ViewGroup.LayoutParams lp)
Should be called instead of
Activity.setContentView(android.view.View, android.view.ViewGroup.LayoutParams) } |
static void |
setDefaultNightMode(int mode)
Sets the default night mode.
|
abstract void |
setHandleNativeActionModesEnabled(boolean enabled)
Whether AppCompat handles any native action modes itself.
|
abstract void |
setLocalNightMode(int mode)
Override the night mode used for this delegate's host component.
|
abstract void |
setSupportActionBar(Toolbar toolbar)
|
abstract void |
setTitle(CharSequence title)
Should be called from
Activity.onTitleChanged(CharSequence, int) } |
abstract ActionMode |
startSupportActionMode(ActionMode.Callback callback)
Start an action mode.
|
public static final int MODE_NIGHT_NO
notnight
qualified
resources where available, regardless of the time.setLocalNightMode(int)
,
Constant Field Valuespublic static final int MODE_NIGHT_YES
night
qualified
resources where available, regardless of the time.setLocalNightMode(int)
,
Constant Field Valuespublic static final int MODE_NIGHT_AUTO
The calculation used to determine whether it is night or not makes use of the location APIs (if this app has the necessary permissions). This allows us to generate accurate sunrise and sunset times. If this app does not have permission to access the location APIs then we use hardcoded times which will be less accurate.
setLocalNightMode(int)
,
Constant Field Valuespublic static final int MODE_NIGHT_FOLLOW_SYSTEM
setLocalNightMode(int)
,
Constant Field Valuespublic static final int FEATURE_SUPPORT_ACTION_BAR
This is enabled by default for some devices. The Action Bar replaces the title bar and provides an alternate location for an on-screen menu button on some devices.
public static final int FEATURE_SUPPORT_ACTION_BAR_OVERLAY
FEATURE_SUPPORT_ACTION_BAR
it will be layered over
the window content itself. This is useful if you would like your app to have more control
over how the Action Bar is displayed, such as letting application content scroll beneath
an Action Bar with a transparent background or otherwise displaying a transparent/translucent
Action Bar over application content.
This mode is especially useful with View.SYSTEM_UI_FLAG_FULLSCREEN
, which allows
you to seamlessly hide the action bar in conjunction with other screen decorations.
When an ActionBar is in this mode it will adjust the insets provided to
View.fitSystemWindows(Rect)
to include the content covered by the action bar, so you can do layout within
that space.
public static final int FEATURE_ACTION_MODE_OVERLAY
public static AppCompatDelegate create(Activity activity, AppCompatCallback callback)
AppCompatDelegate
to use with activity
.callback
- An optional callback for AppCompat specific eventspublic static AppCompatDelegate create(Dialog dialog, AppCompatCallback callback)
AppCompatDelegate
to use with dialog
.callback
- An optional callback for AppCompat specific events@Nullable public abstract ActionBar getSupportActionBar()
Activity.getActionBar()
.public abstract void setSupportActionBar(@Nullable Toolbar toolbar)
Toolbar
to act as the ActionBar
for this delegate.
When set to a non-null value the getSupportActionBar()
()} method will return
an ActionBar
object that can be used to control the given toolbar as if it were
a traditional window decor action bar. The toolbar's menu will be populated with the
Activity's options menu and the navigation button will be wired through the standard
home
menu select action.
In order to use a Toolbar within the Activity's window content the application
must not request the window feature
FEATURE_SUPPORT_ACTION_BAR
.
toolbar
- Toolbar to set as the Activity's action bar, or null
to clear itpublic abstract MenuInflater getMenuInflater()
Activity.getMenuInflater()
public abstract void onCreate(Bundle savedInstanceState)
Activity.onCreate()
.
This should be called before super.onCreate()
as so:
protected void onCreate(Bundle savedInstanceState) { getDelegate().onCreate(savedInstanceState); super.onCreate(savedInstanceState); // ... }
public abstract void onPostCreate(Bundle savedInstanceState)
Activity.onPostCreate(android.os.Bundle)
public abstract void onConfigurationChanged(Configuration newConfig)
Activity.onConfigurationChanged(android.content.res.Configuration)
public abstract void onStart()
Activity.onStart()
Activity.onStart()}public abstract void onStop()
Activity.onStop()
public abstract void onPostResume()
Activity.onPostResume()
@Nullable public abstract View findViewById(@IdRes int id)
onCreate(android.os.Bundle)
.public abstract void setContentView(View v)
Activity.setContentView(android.view.View)
}public abstract void setContentView(@LayoutRes int resId)
Activity.setContentView(int)
}public abstract void setContentView(View v, ViewGroup.LayoutParams lp)
Activity.setContentView(android.view.View, android.view.ViewGroup.LayoutParams)
}public abstract void addContentView(View v, ViewGroup.LayoutParams lp)
Activity.addContentView(android.view.View, android.view.ViewGroup.LayoutParams)
}public abstract void setTitle(@Nullable CharSequence title)
Activity.onTitleChanged(CharSequence, int)
}public abstract void invalidateOptionsMenu()
Activity.invalidateOptionsMenu()
} or
FragmentActivity.supportInvalidateOptionsMenu()
.public abstract void onDestroy()
Activity.onDestroy()
@Nullable public abstract ActionBarDrawerToggle.Delegate getDrawerToggleDelegate()
ActionBarDrawerToggle.Delegate
which can be returned from your Activity
if it implements ActionBarDrawerToggle.DelegateProvider
.public abstract boolean requestWindowFeature(int featureId)
Activity.requestWindowFeature(int)
or
getWindow().requestFeature()
.featureId
- The desired feature as defined in Window
.public abstract boolean hasWindowFeature(int featureId)
This should be called instead of Window.hasFeature(int)
.
featureId
- The feature ID to check@Nullable public abstract ActionMode startSupportActionMode(@NonNull ActionMode.Callback callback)
callback
- Callback that will manage lifecycle events for this context modepublic abstract void installViewFactory()
LayoutInflater
Factory so that it can replace
the framework widgets with compatible tinted versions. This should be called before
super.onCreate()
as so:
protected void onCreate(Bundle savedInstanceState) { getDelegate().installViewFactory(); getDelegate().onCreate(savedInstanceState); super.onCreate(savedInstanceState); // ... }If you are using your own
Factory
or
Factory2
then you can omit this call, and instead call
createView(android.view.View, String, android.content.Context, android.util.AttributeSet)
from your factory to return any compatible widgets.public abstract View createView(@Nullable View parent, String name, @NonNull Context context, @NonNull AttributeSet attrs)
LayoutInflaterFactory
in order
to return tint-aware widgets.
This is only needed if you are using your own
LayoutInflater
factory, and have therefore not
installed the default factory via installViewFactory()
.
public abstract void setHandleNativeActionModesEnabled(boolean enabled)
This methods only takes effect on
Build.VERSION_CODES.ICE_CREAM_SANDWICH
and above.
enabled
- whether AppCompat should handle native action modes.public abstract boolean isHandleNativeActionModesEnabled()
public abstract void onSaveInstanceState(Bundle outState)
public abstract boolean applyDayNight()
night
and notnight
resource qualifiers.
Doing this enables the
Theme.AppCompat.DayNight
family of themes to work, using the computed twilight to automatically select a dark or
light theme.
You can override the night mode using setLocalNightMode(int)
.
This only works on devices running
ICE_CREAM_SANDWICH
and above.
If this is called after the host component has been created, the component will either be
automatically recreated or its Configuration
updated. Which one depends on how
the component is setup (via android:configChanges
or similar).
setDefaultNightMode(int)
,
setLocalNightMode(int)
public abstract void setLocalNightMode(int mode)
applyDayNight()
works.
As this will call applyDayNight()
, the host component might be
recreated automatically.
public static void setDefaultNightMode(int mode)
setLocalNightMode(int)
.
This method only takes effect for those situations where applyDayNight()
works.
Defaults to MODE_NIGHT_NO
.
This only takes effect for components which are created after the call. Any components which are already open will not be updated.
setLocalNightMode(int)
,
getDefaultNightMode()
public static int getDefaultNightMode()
setDefaultNightMode(int)
public static void setCompatVectorFromResourcesEnabled(boolean enabled)
DrawableContainer
resources.
When enabled, AppCompat can intercept some drawable inflation from the framework, which
enables implicit inflation of vector drawables within
DrawableContainer
resources. You can then use those
drawables in places such as android:src
on ImageView
,
or android:drawableLeft
on TextView
. Example usage:
<selector xmlns:android="..."> <item android:state_checked="true" android:drawable="@drawable/vector_checked_icon" /> <item android:drawable="@drawable/vector_icon" /> </selector> <TextView ... android:drawableLeft="@drawable/vector_state_list_icon" />
This feature defaults to disabled, since enabling it can cause issues with memory usage,
and problems updating Configuration
instances. If you update the configuration
manually, then you probably do not want to enable this. You have been warned.
Even with this disabled, you can still use vector resources through
AppCompatImageView.setImageResource(int)
and it's
app:srcCompat
attribute. They can also be used in anything which AppCompat inflates
for you, such as menu resources.
Please note: this only takes effect in Activities created after this call.
public static boolean isCompatVectorFromResourcesEnabled()