FragmentHostCallback
public
abstract
class
FragmentHostCallback
extends FragmentContainer
java.lang.Object | ||
↳ | androidx.fragment.app.FragmentContainer | |
↳ | androidx.fragment.app.FragmentHostCallback<E> |
Integration points with the Fragment host.
Fragments may be hosted by any object; such as an Activity
. In order to
host fragments, implement FragmentHostCallback
, overriding the methods
applicable to the host.
FragmentManager changes its behavior based on what optional interfaces your FragmentHostCallback implements. This includes the following:
ActivityResultRegistryOwner
: Removes the need to overrideonStartIntentSenderFromFragment(Fragment, IntentSender, int, Intent, int, int, int, Bundle)
oronRequestPermissionsFromFragment(Fragment, String[], int)
.FragmentOnAttachListener
: Removes the need to manually callFragmentManager.addFragmentOnAttachListener(FragmentOnAttachListener)
from your host in order to receiveFragmentOnAttachListener.onAttachFragment(FragmentManager, Fragment)
callbacks for theFragmentController.getSupportFragmentManager()
.OnBackPressedDispatcherOwner
: Removes the need to manually callFragmentManager.popBackStackImmediate()
when handling the system back button.ViewModelStoreOwner
: Removes the need for yourFragmentController
to callFragmentController.retainNestedNonConfig()
orFragmentController.restoreAllState(Parcelable, FragmentManagerNonConfig)
.
Summary
Public constructors | |
---|---|
FragmentHostCallback(Context context, Handler handler, int windowAnimations)
|
Public methods | |
---|---|
void
|
onDump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args)
Print internal state into the given stream. |
View
|
onFindViewById(int id)
Return the view with the given resource ID. |
abstract
E
|
onGetHost()
Return the object that's currently hosting the fragment. |
LayoutInflater
|
onGetLayoutInflater()
Return a |
int
|
onGetWindowAnimations()
Return the window animations. |
boolean
|
onHasView()
Return |
boolean
|
onHasWindowAnimations()
Return |
void
|
onRequestPermissionsFromFragment(Fragment fragment, String[] permissions, int requestCode)
This method is deprecated.
Have your FragmentHostCallback implement |
boolean
|
onShouldSaveFragmentState(Fragment fragment)
Return |
boolean
|
onShouldShowRequestPermissionRationale(String permission)
Checks whether to show permission rationale UI from a fragment. |
void
|
onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode, Bundle options)
Starts a new |
void
|
onStartActivityFromFragment(Fragment fragment, Intent intent, int requestCode)
Starts a new |
void
|
onStartIntentSenderFromFragment(Fragment fragment, IntentSender intent, int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options)
This method is deprecated.
Have your FragmentHostCallback implement |
void
|
onSupportInvalidateOptionsMenu()
Invalidates the activity's options menu. |
Inherited methods | |
---|---|
Public constructors
FragmentHostCallback
public FragmentHostCallback (Context context, Handler handler, int windowAnimations)
Parameters | |
---|---|
context |
Context |
handler |
Handler |
windowAnimations |
int |
Public methods
onDump
public void onDump (String prefix, FileDescriptor fd, PrintWriter writer, String[] args)
Print internal state into the given stream.
Parameters | |
---|---|
prefix |
String : Desired prefix to prepend at each line of output. |
fd |
FileDescriptor : The raw file descriptor that the dump is being sent to. |
writer |
PrintWriter : The PrintWriter to which you should dump your state. This will be closed
for you after you return. |
args |
String : additional arguments to the dump request.
|
onFindViewById
public View onFindViewById (int id)
Return the view with the given resource ID. May return null
if the
view is not a child of this container.
Parameters | |
---|---|
id |
int |
Returns | |
---|---|
View |
onGetHost
public abstract E onGetHost ()
Return the object that's currently hosting the fragment. If a Fragment
is hosted by a FragmentActivity
, the object returned here should be
the same object returned from Fragment.getActivity()
.
Returns | |
---|---|
E |
onGetLayoutInflater
public LayoutInflater onGetLayoutInflater ()
Return a LayoutInflater
.
See Activity.getLayoutInflater()
.
Returns | |
---|---|
LayoutInflater |
onGetWindowAnimations
public int onGetWindowAnimations ()
Return the window animations.
Returns | |
---|---|
int |
onHasView
public boolean onHasView ()
Return true
if the container holds any view.
Returns | |
---|---|
boolean |
onHasWindowAnimations
public boolean onHasWindowAnimations ()
Return true
if there are window animations.
Returns | |
---|---|
boolean |
onRequestPermissionsFromFragment
public void onRequestPermissionsFromFragment (Fragment fragment, String[] permissions, int requestCode)
This method is deprecated.
Have your FragmentHostCallback implement ActivityResultRegistryOwner
to allow Fragments to use
Fragment.registerForActivityResult(ActivityResultContract, ActivityResultCallback)
with ActivityResultContracts.RequestMultiplePermissions
. This method will still be called when Fragments
call the deprecated requestPermissions()
method.
Requests permissions from the given fragment.
See Activity.requestPermissions(String[], int)
Parameters | |
---|---|
fragment |
Fragment |
permissions |
String |
requestCode |
int |
onShouldSaveFragmentState
public boolean onShouldSaveFragmentState (Fragment fragment)
Return true
if the fragment's state needs to be saved.
Parameters | |
---|---|
fragment |
Fragment |
Returns | |
---|---|
boolean |
onShouldShowRequestPermissionRationale
public boolean onShouldShowRequestPermissionRationale (String permission)
Checks whether to show permission rationale UI from a fragment.
See Activity.shouldShowRequestPermissionRationale(String)
Parameters | |
---|---|
permission |
String |
Returns | |
---|---|
boolean |
onStartActivityFromFragment
public void onStartActivityFromFragment (Fragment fragment, Intent intent, int requestCode, Bundle options)
Starts a new Activity
from the given fragment.
See ComponentActivity.startActivityForResult(Intent, int, Bundle)
.
Parameters | |
---|---|
fragment |
Fragment |
intent |
Intent |
requestCode |
int |
options |
Bundle |
onStartActivityFromFragment
public void onStartActivityFromFragment (Fragment fragment, Intent intent, int requestCode)
Starts a new Activity
from the given fragment.
See ComponentActivity.startActivityForResult(Intent, int)
.
Parameters | |
---|---|
fragment |
Fragment |
intent |
Intent |
requestCode |
int |
onStartIntentSenderFromFragment
public void onStartIntentSenderFromFragment (Fragment fragment, IntentSender intent, int requestCode, Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options)
This method is deprecated.
Have your FragmentHostCallback implement ActivityResultRegistryOwner
to allow Fragments to use
Fragment.registerForActivityResult(ActivityResultContract, ActivityResultCallback)
with ActivityResultContracts.StartIntentSenderForResult
. This method will still be called when Fragments
call the deprecated startIntentSenderForResult()
method.
Starts a new IntentSender
from the given fragment.
See Activity.startIntentSender(IntentSender, Intent, int, int, int, Bundle)
.
Parameters | |
---|---|
fragment |
Fragment |
intent |
IntentSender |
requestCode |
int |
fillInIntent |
Intent |
flagsMask |
int |
flagsValues |
int |
extraFlags |
int |
options |
Bundle |
Throws | |
---|---|
IntentSender.SendIntentException |
onSupportInvalidateOptionsMenu
public void onSupportInvalidateOptionsMenu ()
Invalidates the activity's options menu.
See FragmentActivity.supportInvalidateOptionsMenu()