{"id":300278,"date":"2020-03-17T15:00:48","date_gmt":"2020-03-17T15:00:48","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=300278"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=300278","title":{"rendered":"\u0420\u0430\u0441\u043a\u043b\u0430\u0434\u044b\u0432\u0430\u0435\u043c \u043d\u0430 \u0447\u0430\u0441\u0442\u0438 FragmentLifecycleCallbacks"},"content":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/company\/yamoney\/blog\/492272\/\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442! \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0447\u0435\u043c\u0443-\u0442\u043e \u043e\u0431\u0434\u0435\u043b\u0438\u043b\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435\u043c. \u0412 \u0441\u0432\u043e\u0435\u0439 <a href=\"https:\/\/habr.com\/ru\/company\/yamoney\/blog\/482476\/\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a> \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u043f\u0440\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 ActivityLifecycleCallbacks \u0438 \u043a\u0430\u043a \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430. \u041d\u043e \u043a\u0440\u043e\u043c\u0435 Activity \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u0438 Fragment, \u0438 \u043d\u0430\u043c \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u044f \u043d\u0438\u0445 \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435.<\/p>\n<p>  <\/p>\n<p>\u041d\u0435 \u0434\u043e\u043b\u0433\u043e \u0434\u0443\u043c\u0430\u044f, \u044f \u043e\u0442\u043a\u0440\u044b\u043b \u043f\u043e\u0438\u0441\u043a \u043f\u043e \u043a\u043b\u0430\u0441\u0441\u0430\u043c \u0432 AndroidStudio (Cmd\/Ctrl + O) \u0438 \u0432\u0432\u0435\u043b \u0442\u0443\u0434\u0430 FragmentLifecycleCallbacks. \u0418 \u043a\u0430\u043a\u043e\u0432\u043e \u0436\u0435 \u0431\u044b\u043b\u043e \u043c\u043e\u0435 \u0443\u0434\u0438\u0432\u043b\u0435\u043d\u0438\u0435, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u0438\u0441\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043b \u043c\u043d\u0435 FragmentManager.FragmentLifecycleCallbacks. \u0421\u0430\u043c\u044b\u0435 \u043d\u0435\u0442\u0435\u0440\u043f\u0435\u043b\u0438\u0432\u044b\u0435 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0438 \u043f\u0438\u0441\u0430\u043b\u0438 \u043f\u0440\u043e \u044d\u0442\u043e \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u043e\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0439 \u044d\u0442\u043e\u0439 \u0438\u0441\u0442\u043e\u0440\u0438\u0438. \u0421\u043a\u043e\u0440\u0435\u0435 \u043f\u043e\u0434 \u043a\u0430\u0442!<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/py\/v_\/s4\/pyv_s4vjq97ehajg713vrm_oodm.jpeg\"><\/p>\n<p><a name=\"habracut\"><\/a>  <\/p>\n<h2 id=\"chto-eto-takoe\">\u0427\u0442\u043e \u044d\u0442\u043e \u0442\u0430\u043a\u043e\u0435<\/h2>\n<p>  <\/p>\n<p>\u0418\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441 \u043d\u0430\u043f\u043e\u0434\u043e\u0431\u0438\u0435 ActivityLifecycleCallbacks, \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f Fragment.<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">FragmentLifecycleCallbacks<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"java\">\/**  * Callback interface for listening to fragment state changes that happen  * within a given FragmentManager.  *\/ public abstract static class FragmentLifecycleCallbacks {     \/**      * Called right before the fragment's {@link Fragment#onAttach(Context)} method is called.      * This is a good time to inject any required dependencies or perform other configuration      * for the fragment before any of the fragment's lifecycle methods are invoked.      *      * @param fm Host FragmentManager      * @param f Fragment changing state      * @param context Context that the Fragment is being attached to      *\/     public void onFragmentPreAttached(         @NonNull FragmentManager fm,          @NonNull Fragment f,         @NonNull Context context) {}      \/**      * Called after the fragment has been attached to its host. Its host will have had      * `onAttachFragment` called before this call happens.      *      * @param fm Host FragmentManager      * @param f Fragment changing state      * @param context Context that the Fragment was attached to      *\/     public void onFragmentAttached(         @NonNull FragmentManager fm,         @NonNull Fragment f,         @NonNull Context context) {}      \/**      * Called right before the fragment's {@link Fragment#onCreate(Bundle)} method is called.      * This is a good time to inject any required dependencies or perform other configuration      * for the fragment.      *      * @param fm Host FragmentManager      * @param f Fragment changing state      * @param savedInstanceState Saved instance bundle from a previous instance      *\/     public void onFragmentPreCreated(         @NonNull FragmentManager fm,         @NonNull Fragment f,         @Nullable Bundle savedInstanceState) {}      \/**      * Called after the fragment has returned from the FragmentManager's call to      * {@link Fragment#onCreate(Bundle)}. This will only happen once for any given      * fragment instance, though the fragment may be attached and detached multiple times.      *      * @param fm Host FragmentManager      * @param f Fragment changing state      * @param savedInstanceState Saved instance bundle from a previous instance      *\/     public void onFragmentCreated(         @NonNull FragmentManager fm,         @NonNull Fragment f,         @Nullable Bundle savedInstanceState) {}      \/**      * Called after the fragment has returned from the FragmentManager's call to      * {@link Fragment#onActivityCreated(Bundle)}. This will only happen once for any given      * fragment instance, though the fragment may be attached and detached multiple times.      *      * @param fm Host FragmentManager      * @param f Fragment changing state      * @param savedInstanceState Saved instance bundle from a previous instance      *\/     public void onFragmentActivityCreated(         @NonNull FragmentManager fm,         @NonNull Fragment f,         @Nullable Bundle savedInstanceState) {}      \/**      * Called after the fragment has returned a non-null view from the FragmentManager's      * request to {@link Fragment#onCreateView(LayoutInflater, ViewGroup, Bundle)}.      *      * @param fm Host FragmentManager      * @param f Fragment that created and owns the view      * @param v View returned by the fragment      * @param savedInstanceState Saved instance bundle from a previous instance      *\/     public void onFragmentViewCreated(         @NonNull FragmentManager fm,         @NonNull Fragment f,         @NonNull View v,         @Nullable Bundle savedInstanceState) {}      \/**      * Called after the fragment has returned from the FragmentManager's call to      * {@link Fragment#onStart()}.      *      * @param fm Host FragmentManager      * @param f Fragment changing state      *\/     public void onFragmentStarted(         @NonNull FragmentManager fm,          @NonNull Fragment f) {}      \/**      * Called after the fragment has returned from the FragmentManager's call to      * {@link Fragment#onResume()}.      *      * @param fm Host FragmentManager      * @param f Fragment changing state      *\/     public void onFragmentResumed(         @NonNull FragmentManager fm,          @NonNull Fragment f) {}      \/**      * Called after the fragment has returned from the FragmentManager's call to      * {@link Fragment#onPause()}.      *      * @param fm Host FragmentManager      * @param f Fragment changing state      *\/     public void onFragmentPaused(         @NonNull FragmentManager fm,          @NonNull Fragment f) {}      \/**      * Called after the fragment has returned from the FragmentManager's call to      * {@link Fragment#onStop()}.      *      * @param fm Host FragmentManager      * @param f Fragment changing state      *\/     public void onFragmentStopped(         @NonNull FragmentManager fm,         @NonNull Fragment f) {}      \/**      * Called after the fragment has returned from the FragmentManager's call to      * {@link Fragment#onSaveInstanceState(Bundle)}.      *      * @param fm Host FragmentManager      * @param f Fragment changing state      * @param outState Saved state bundle for the fragment      *\/     public void onFragmentSaveInstanceState(         @NonNull FragmentManager fm,         @NonNull Fragment f,         @NonNull Bundle outState) {}      \/**      * Called after the fragment has returned from the FragmentManager's call to      * {@link Fragment#onDestroyView()}.      *      * @param fm Host FragmentManager      * @param f Fragment changing state      *\/     public void onFragmentViewDestroyed(         @NonNull FragmentManager fm,         @NonNull Fragment f) {}      \/**      * Called after the fragment has returned from the FragmentManager's call to      * {@link Fragment#onDestroy()}.      *      * @param fm Host FragmentManager      * @param f Fragment changing state      *\/     public void onFragmentDestroyed(         @NonNull FragmentManager fm,         @NonNull Fragment f) {}      \/**      * Called after the fragment has returned from the FragmentManager's call to      * {@link Fragment#onDetach()}.      *      * @param fm Host FragmentManager      * @param f Fragment changing state      *\/     public void onFragmentDetached(         @NonNull FragmentManager fm,         @NonNull Fragment f) {} }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u0412 \u043e\u0442\u043b\u0438\u0447\u0438\u0435 \u043e\u0442 ActivityLifecycleCallbacks \u043e\u043d \u0443\u043f\u0440\u0430\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0435 \u0441\u0430\u043c\u0438\u043c Fragment, \u0430 FragmentManager, \u0447\u0442\u043e \u0434\u0430\u0435\u0442 \u0440\u044f\u0434 \u043f\u0440\u0435\u0438\u043c\u0443\u0449\u0435\u0441\u0442\u0432. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u0443 \u044d\u0442\u043e\u0433\u043e \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441\u0430 \u043c\u0435\u0442\u043e\u0434\u044b \u0441 \u043f\u0440\u0438\u0441\u0442\u0430\u0432\u043a\u043e\u0439 Pre-, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u0434\u043e \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0445 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 Fragment. \u0410 \u043c\u0435\u0442\u043e\u0434\u044b \u0431\u0435\u0437 \u043f\u0440\u0438\u0441\u0442\u0430\u0432\u043a\u0438 \u0432\u044b\u0437\u044b\u0432\u0430\u044e\u0442\u0441\u044f \u043f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0441\u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u044d\u0442\u0438 \u0436\u0435 \u043c\u0435\u0442\u043e\u0434\u044b Fragment.<\/p>\n<p>  <\/p>\n<p>\u041a \u0442\u043e\u043c\u0443 \u0436\u0435 FragmentLifecycleCallbacks \u2014 \u044d\u0442\u043e \u0430\u0431\u0441\u0442\u0440\u0430\u043a\u0442\u043d\u044b\u0439 \u043a\u043b\u0430\u0441\u0441, \u0430 \u043d\u0435 \u0438\u043d\u0442\u0435\u0440\u0444\u0435\u0439\u0441. \u0414\u0443\u043c\u0430\u044e, \u0447\u0442\u043e \u044d\u0442\u043e \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u0443 \u043c\u0435\u0442\u043e\u0434\u043e\u0432 \u0431\u044b\u043b\u0430 \u0440\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e.<\/p>\n<p>  <\/p>\n<p>\u041d\u043e \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u043a \u0438\u043d\u0442\u0435\u0440\u0435\u0441\u043d\u043e\u043c\u0443 \u2014 \u043a\u0430\u043a \u044d\u0442\u043e \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u044c.<\/p>\n<p>  <\/p>\n<h2 id=\"kak-zaregistrirovat\">\u041a\u0430\u043a \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c<\/h2>\n<p>  <\/p>\n<p>\u0427\u0442\u043e\u0431\u044b \u0437\u0430\u0441\u0442\u0430\u0432\u0438\u0442\u044c FragmentLifecycleCallbacks \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c, \u0435\u0433\u043e \u043d\u0443\u0436\u043d\u043e \u0437\u0430\u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043d\u0430 FragmentManager. \u0414\u043b\u044f \u044d\u0442\u043e\u0433\u043e \u043d\u0430\u0434\u043e \u0432\u044b\u0437\u0432\u0430\u0442\u044c FragmentManager.registerFragmentLifecycleCallback(), \u043f\u0435\u0440\u0435\u0434\u0430\u0432 \u0432 \u043d\u0435\u0433\u043e \u0434\u0432\u0430 \u043f\u0430\u0440\u0430\u043c\u0435\u0442\u0440\u0430: \u0441\u0430\u043c callback \u0438 \u0444\u043b\u0430\u0433 \u2014 recursive. \u0424\u043b\u0430\u0433 \u043f\u043e\u043a\u0430\u0437\u044b\u0432\u0430\u0435\u0442, \u043d\u0443\u0436\u043d\u043e \u043b\u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u0438\u0442\u044c \u044d\u0442\u043e\u0442 callback \u0442\u043e\u043b\u044c\u043a\u043e \u043a \u044d\u0442\u043e\u043c\u0443 FragmentManager \u0438\u043b\u0438 \u0435\u0433\u043e \u043d\u0430\u0434\u043e \u0440\u0435\u043a\u0443\u0440\u0441\u0438\u0432\u043d\u043e \u043f\u0440\u043e\u043a\u0438\u0434\u044b\u0432\u0430\u0442\u044c \u0432\u043e \u0432\u0441\u0435 childFragmentManager\u2019\u044b, \u044d\u0442\u043e\u0433\u043e FragmentManager&#8217;\u0430 \u0438 \u0434\u0430\u043b\u044c\u0448\u0435 \u043f\u043e \u0438\u0435\u0440\u0430\u0440\u0445\u0438\u0438.<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/an\/pk\/pk\/anpkpk0jafe_6tt-xo-zgd72ape.jpeg\"><\/p>\n<p>  <\/p>\n<p>FragmentLifecycleCallback \u0441\u0442\u043e\u0438\u0442 \u0440\u0435\u0433\u0438\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0434\u043e Activity.onCreate(), \u0438\u043d\u0430\u0447\u0435 \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043d\u0435 \u0432\u0441\u0435 \u0441\u043e\u0431\u044b\u0442\u0438\u044f, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u0440\u0438 \u0432\u043e\u0441\u0441\u0442\u0430\u043d\u043e\u0432\u043b\u0435\u043d\u0438\u0438 \u0441\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f.<\/p>\n<p>  <\/p>\n<pre><code class=\"kotlin\">class FlcExampleActivity : AppCompatActivity() {     override fun onCreate(savedInstanceState: Bundle?) {         supportFragmentManager             .registerFragmentLifecycleCallbacks(                 ExampleFragmentLifecycleCallback(),                 true             )          super.onCreate(savedInstanceState)     } }  class ExampleFragmentLifecycleCallback : FragmentManager.FragmentLifecycleCallbacks()<\/code><\/pre>\n<p>  <\/p>\n<p>\u0412\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u043d\u0435 \u043e\u0447\u0435\u043d\u044c \u043a\u0440\u0430\u0441\u0438\u0432\u043e, \u0438 \u0432 \u043d\u0435\u043a\u043e\u0442\u043e\u0440\u044b\u0445 \u0441\u0438\u0442\u0443\u0430\u0446\u0438\u044f\u0445 \u043f\u043e\u0442\u0440\u0435\u0431\u0443\u0435\u0442 \u0437\u0430\u0432\u043e\u0434\u0438\u0442\u044c \u0447\u0442\u043e-\u0442\u043e \u0432\u0440\u043e\u0434\u0435 \u0431\u0430\u0437\u043e\u0432\u043e\u0439 Activity. \u041d\u043e \u0435\u0441\u043b\u0438 \u0442\u044b \u0443\u0436\u0435 \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b \u043c\u043e\u044e <a href=\"https:\/\/habr.com\/ru\/company\/yamoney\/blog\/482476\/\">\u0441\u0442\u0430\u0442\u044c\u044e \u043f\u0440\u043e ActivityLifecycleCallbacks<\/a>, \u0442\u043e \u0437\u043d\u0430\u0435\u0448\u044c, \u043d\u0430 \u0447\u0442\u043e \u0431\u0430\u0437\u043e\u0432\u044b\u0435 Activity \u043e\u0442\u043b\u0438\u0447\u043d\u043e \u0437\u0430\u043c\u0435\u043d\u044f\u044e\u0442\u0441\u044f =).<\/p>\n<p>  <\/p>\n<pre><code class=\"kotlin\">class ActivityFragmentLifecycleCallbacks :     Application.ActivityLifecycleCallbacks,     FragmentManager.FragmentLifecycleCallbacks() {      override fun onActivityCreated(         activity: Activity,         savedInstanceState: Bundle?     ) {         (activity as? FragmentActivity)             ?.supportFragmentManager             ?.registerFragmentLifecycleCallbacks(this, true)     } }<\/code><\/pre>\n<p>  <\/p>\n<p>\u0418 \u0442\u0443\u0442 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u043c \u043f\u043e\u0442\u0440\u044f\u0441\u0430\u044e\u0449\u0443\u044e \u0441\u0438\u043d\u0435\u0440\u0433\u0438\u044e callback\u2019\u043e\u0432. \u0411\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f \u044d\u0442\u043e\u043c\u0443 \u0440\u0435\u0448\u0435\u043d\u0438\u044e \u043c\u044b \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u0435\u043c \u0434\u043e\u0442\u044f\u043d\u0443\u0442\u044c\u0441\u044f \u043f\u043e\u0447\u0442\u0438 \u0434\u043e \u043b\u044e\u0431\u043e\u0433\u043e \u043e\u0431\u044a\u0435\u043a\u0442\u0430 Activity \u0438 Fragment, \u0441\u043e\u0437\u0434\u0430\u0432\u0430\u0435\u043c\u044b\u0445 \u0432 \u0441\u0438\u0441\u0442\u0435\u043c\u0435. \u0418 \u0442\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u0432\u0441\u0435 \u043a\u0430\u043a \u043d\u0430 \u043b\u0430\u0434\u043e\u043d\u0438, \u043c\u043e\u0436\u043d\u043e \u0437\u0430\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0432\u0441\u044e \u044d\u0442\u0443 \u0441\u0438\u0441\u0442\u0435\u043c\u0443 \u0440\u0430\u0431\u043e\u0442\u0430\u0442\u044c \u043d\u0430 \u043d\u0430\u0441.<\/p>\n<p>  <\/p>\n<h2 id=\"primery-ispolzovaniya\">\u041f\u0440\u0438\u043c\u0435\u0440\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f<\/h2>\n<p>  <\/p>\n<p>\u0421\u0440\u0430\u0437\u0443 \u043f\u0440\u043e dependency injection: \u0434\u0430, \u0442\u0435\u043f\u0435\u0440\u044c \u043c\u043e\u0436\u043d\u043e \u0440\u0430\u0441\u043f\u0440\u043e\u0441\u0442\u0440\u0430\u043d\u044f\u0442\u044c \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043f\u043e \u0432\u0441\u0435\u043c\u0443 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u044e, \u0434\u0430\u0436\u0435 \u0435\u0441\u043b\u0438 \u0443 \u0432\u0430\u0441 Single Activity Application. \u041f\u043e\u043c\u043d\u0438\u0448\u044c \u043f\u0440\u0438\u043c\u0435\u0440 \u0438\u0437 <a href=\"https:\/\/habr.com\/ru\/company\/yamoney\/blog\/482476\/\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0438<\/a>, \u043f\u0440\u043e RequireCoolTool? \u0422\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435 \u043c\u043e\u0436\u043d\u043e \u0441\u0434\u0435\u043b\u0430\u0442\u044c \u0434\u043b\u044f \u0432\u0441\u0435\u0445 Activity \u0438 Fragment \u0432 \u043f\u0440\u0438\u043b\u043e\u0436\u0435\u043d\u0438\u0438. \u0418 \u0442\u044b \u0443\u0436\u0435 \u0434\u043e\u0433\u0430\u0434\u0430\u043b\u0441\u044f \u043a\u0430\u043a, \u0434\u0430? \u041d\u043e \u044f \u0432\u0441\u0435-\u0440\u0430\u0432\u043d\u043e \u043f\u043e\u043a\u0430\u0436\u0443 \u043f\u0440\u0438\u043c\u0435\u0440.<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">Dependency injection<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"kotlin\">interface CoolTool {     val extraInfo: String }  class CoolToolImpl : CoolTool {     override val extraInfo = &quot;i am dependency&quot; }  interface RequireCoolTool {     var coolTool: CoolTool }  class InjectingLifecycleCallbacks :     Application.ActivityLifecycleCallbacks,     FragmentManager.FragmentLifecycleCallbacks() {      private val coolToolImpl = CoolToolImpl()      override fun onActivityCreated(         activity: Activity,         savedInstanceState: Bundle?     ) {         (activity as? RequireCoolTool)?.coolTool = coolToolImpl         (activity as? FragmentActivity)             ?.supportFragmentManager             ?.registerFragmentLifecycleCallbacks(this, true)     }      override fun onFragmentPreCreated(         fm: FragmentManager,         f: Fragment,         savedInstanceState: Bundle?     ) {         (f as? RequireCoolTool)?.coolTool = coolToolImpl     } }  class DIActivity : AppCompatActivity(), RequireCoolTool {      override lateinit var coolTool: CoolTool      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)          setContentView(LinearLayout {             orientation = LinearLayout.VERTICAL             FrameLayout {                 layoutParams = LinearLayout.LayoutParams(                     LinearLayout.LayoutParams.MATCH_PARENT, 0, 1f)                 Text(                     &quot;&quot;&quot;                     DI example activity                     CoolTool.extraInfo=&quot;${coolTool.extraInfo}&quot;                     &quot;&quot;&quot;.trimIndent()                 )             }             FrameLayout {                 layoutParams = LinearLayout.LayoutParams(                     LinearLayout.LayoutParams.MATCH_PARENT, 0, 1f)                 id = R.id.container             }         })          supportFragmentManager.findFragmentById(R.id.container) ?: run {             supportFragmentManager                 .beginTransaction()                 .add(R.id.container, DIFragment())                 .commit()         }     } }  class DIFragment : Fragment(), RequireCoolTool {      override lateinit var coolTool: CoolTool      override fun onCreateView(         inflater: LayoutInflater,         container: ViewGroup?,         savedInstanceState: Bundle?     ): View? =         inflater.context.FrameLayout {             setBackgroundColor(Color.LTGRAY)             Text(                 &quot;&quot;&quot;                     DI example fragment                     CoolTool.extraInfo=&quot;${coolTool.extraInfo}&quot;                     &quot;&quot;&quot;.trimIndent()             )         }  }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u0418 \u043a\u043e\u043d\u0435\u0447\u043d\u043e \u0436\u0435 \u0441 Dagger\u2019\u043e\u043c \u0432\u0441\u0435 \u0442\u043e\u0436\u0435 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442.<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">Dagger<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"kotlin\">interface DaggerTool {     val extraInfo: String }  class DaggerToolImpl : DaggerTool {     override val extraInfo = &quot;i am dependency&quot; }  class DaggerInjectingLifecycleCallbacks(     val dispatchingAndroidInjector: DispatchingAndroidInjector&lt;Any&gt; ) : Application.ActivityLifecycleCallbacks,     FragmentManager.FragmentLifecycleCallbacks() {      override fun onActivityCreated(         activity: Activity,         savedInstanceState: Bundle?     ) {         dispatchingAndroidInjector.maybeInject(activity)         (activity as? FragmentActivity)             ?.supportFragmentManager             ?.registerFragmentLifecycleCallbacks(this, true)     }      override fun onFragmentPreCreated(         fm: FragmentManager,         f: Fragment,         savedInstanceState: Bundle?     ) {         dispatchingAndroidInjector.maybeInject(f)     } }  class DaggerActivity : AppCompatActivity() {      @Inject     lateinit var daggerTool: DaggerTool      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)          setContentView(LinearLayout {             orientation = LinearLayout.VERTICAL             FrameLayout {                 layoutParams = LinearLayout.LayoutParams(                     LinearLayout.LayoutParams.MATCH_PARENT, 0, 1f)                 Text(                     &quot;&quot;&quot;                     Dagger example activity                     CoolTool.extraInfo=&quot;${daggerTool.extraInfo}&quot;                     &quot;&quot;&quot;.trimIndent()                 )             }             FrameLayout {                 layoutParams = LinearLayout.LayoutParams(                     LinearLayout.LayoutParams.MATCH_PARENT, 0, 1f)                 id = R.id.container             }         })          supportFragmentManager.findFragmentById(R.id.container) ?: run {             supportFragmentManager                 .beginTransaction()                 .add(R.id.container, DIFragment())                 .commit()         }     } }  class DaggerFragment : Fragment() {      @Inject     lateinit var daggerTool: DaggerTool      override fun onCreateView(         inflater: LayoutInflater,         container: ViewGroup?,         savedInstanceState: Bundle?     ): View? =         inflater.context.FrameLayout {             Text(                 &quot;&quot;&quot;                 Dagger example fragment                 DaggerTool.extraInfo=&quot;${daggerTool.extraInfo}&quot;                 &quot;&quot;&quot;.trimIndent()             )         } }  @Module class DaggerModule {     @Provides     fun provideDaggerTool(): DaggerTool {         return DaggerToolImpl()     } }  @Module abstract class DaggerAndroidModule {     @ContributesAndroidInjector(modules = [DaggerModule::class])     abstract fun contributeDaggerActivity(): DaggerActivity      @ContributesAndroidInjector(modules = [DaggerModule::class])     abstract fun contributeDaggerFragment(): DaggerFragment }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u042f \u0434\u0443\u043c\u0430\u044e, \u0447\u0442\u043e \u0442\u044b \u0432\u043f\u043e\u043b\u043d\u0435 \u0441\u043f\u0440\u0430\u0432\u0438\u0448\u044c\u0441\u044f \u0441 \u0434\u0440\u0443\u0433\u0438\u043c\u0438 DI-\u0444\u0440\u0435\u0439\u043c\u0432\u043e\u0440\u043a\u0430\u043c\u0438, \u043d\u043e \u0435\u0441\u043b\u0438 \u043d\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u0441\u044f, \u0442\u043e \u0434\u0430\u0432\u0430\u0439 \u043e\u0431\u0441\u0443\u0434\u0438\u043c \u044d\u0442\u043e \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445.\u00a0<\/p>\n<p>  <\/p>\n<p>\u041a\u043e\u043d\u0435\u0447\u043d\u043e, \u043c\u043e\u0436\u043d\u043e \u0434\u0435\u043b\u0430\u0442\u044c \u0432\u0441\u0435 \u0442\u043e \u0436\u0435 \u0441\u0430\u043c\u043e\u0435, \u0447\u0442\u043e \u0438 \u0441 Activity, \u043d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043e\u0442\u043f\u0440\u0430\u0432\u043b\u044f\u0442\u044c \u0430\u043d\u0430\u043b\u0438\u0442\u0438\u043a\u0443.<\/p>\n<p>  <\/p>\n<div class=\"spoiler\"><b class=\"spoiler_title\">Analytics<\/b><\/p>\n<div class=\"spoiler_text\">\n<pre><code class=\"kotlin\">interface Screen {     val screenName: String }  interface ScreenWithParameters : Screen {     val parameters: Map&lt;String, String&gt; }  class AnalyticsCallback(     val sendAnalytics: (String, Map&lt;String, String&gt;?) -&gt; Unit ) : Application.ActivityLifecycleCallbacks,      FragmentManager.FragmentLifecycleCallbacks() {      override fun onActivityCreated(         activity: Activity,         savedInstanceState: Bundle?     ) {         if (savedInstanceState == null) {             (activity as? Screen)?.screenName?.let {                 sendAnalytics(                     it,                     (activity as? ScreenWithParameters)?.parameters                 )             }         }     } }  class AnalyticsActivity : AppCompatActivity(), ScreenWithParameters {      override val screenName: String = &quot;First screen&quot;      override val parameters: Map&lt;String, String&gt; = mapOf(&quot;key&quot; to &quot;value&quot;)      override fun onCreate(savedInstanceState: Bundle?) {         super.onCreate(savedInstanceState)          setContentView(LinearLayout {             orientation = android.widget.LinearLayout.VERTICAL             FrameLayout {                 layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f)                 Text(                     &quot;&quot;&quot;                     Analytics example                     see output in Logcat by &quot;Analytics&quot; tag                     &quot;&quot;&quot;.trimIndent()                 )             }             FrameLayout {                 layoutParams = LayoutParams(LayoutParams.MATCH_PARENT, 0, 1f)                 id = R.id.container             }         })          with(supportFragmentManager) {             findFragmentById(R.id.container) ?: commit {                 add(R.id.container, AnalyticsFragment())             }         }     } }  class AnalyticsFragment : Fragment(), ScreenWithParameters {      override val screenName: String = &quot;Fragment screen&quot;      override val parameters: Map&lt;String, String&gt; = mapOf(&quot;key&quot; to &quot;value&quot;)      override fun onCreateView(         inflater: LayoutInflater,         container: ViewGroup?,         savedInstanceState: Bundle?     ): View? =         inflater.context.FrameLayout {             setBackgroundColor(Color.LTGRAY)             Text(                 &quot;&quot;&quot;                 Analytics example                 see output in Logcat by &quot;Analytics&quot; tag                 &quot;&quot;&quot;.trimIndent()             )         } }<\/code><\/pre>\n<\/div>\n<\/div>\n<p>  <\/p>\n<p>\u0410 \u043a\u0430\u043a\u0438\u0435 \u0432\u0430\u0440\u0438\u0430\u043d\u0442\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u043d\u0438\u044f \u0437\u043d\u0430\u0435\u0448\u044c \u0442\u044b?<\/p>\n<\/div>\n<p> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/company\/yamoney\/blog\/492272\/\"> https:\/\/habr.com\/ru\/company\/yamoney\/blog\/492272\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"\n<div class=\"post__text post__text-html post__text_v1\" id=\"post-content-body\" data-io-article-url=\"https:\/\/habr.com\/ru\/company\/yamoney\/blog\/492272\/\">\n<p>\u041f\u0440\u0438\u0432\u0435\u0442! \u0421\u0435\u0433\u043e\u0434\u043d\u044f \u044f \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0430\u044e \u0440\u0430\u0441\u0441\u043a\u0430\u0437\u044b\u0432\u0430\u0442\u044c \u043f\u0440\u043e \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u044b, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u0447\u0435\u043c\u0443-\u0442\u043e \u043e\u0431\u0434\u0435\u043b\u0438\u043b\u0438 \u0432\u043d\u0438\u043c\u0430\u043d\u0438\u0435\u043c. \u0412 \u0441\u0432\u043e\u0435\u0439 <a href=\"https:\/\/habr.com\/ru\/company\/yamoney\/blog\/482476\/\">\u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0441\u0442\u0430\u0442\u044c\u0435<\/a> \u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043b \u043f\u0440\u043e \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u0438 ActivityLifecycleCallbacks \u0438 \u043a\u0430\u043a \u0438\u0445 \u043c\u043e\u0436\u043d\u043e \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043b\u043e\u0433\u0438\u0440\u043e\u0432\u0430\u043d\u0438\u044f \u0436\u0438\u0437\u043d\u0435\u043d\u043d\u043e\u0433\u043e \u0446\u0438\u043a\u043b\u0430. \u041d\u043e \u043a\u0440\u043e\u043c\u0435 Activity \u0435\u0441\u0442\u044c \u0435\u0449\u0435 \u0438 Fragment, \u0438 \u043d\u0430\u043c \u0445\u043e\u0442\u0435\u043b\u043e\u0441\u044c \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u0434\u043b\u044f \u043d\u0438\u0445 \u043f\u043e\u0434\u043e\u0431\u043d\u043e\u0435 \u043f\u043e\u0432\u0435\u0434\u0435\u043d\u0438\u0435.<\/p>\n<p>  <\/p>\n<p>\u041d\u0435 \u0434\u043e\u043b\u0433\u043e \u0434\u0443\u043c\u0430\u044f, \u044f \u043e\u0442\u043a\u0440\u044b\u043b \u043f\u043e\u0438\u0441\u043a \u043f\u043e \u043a\u043b\u0430\u0441\u0441\u0430\u043c \u0432 AndroidStudio (Cmd\/Ctrl + O) \u0438 \u0432\u0432\u0435\u043b \u0442\u0443\u0434\u0430 FragmentLifecycleCallbacks. \u0418 \u043a\u0430\u043a\u043e\u0432\u043e \u0436\u0435 \u0431\u044b\u043b\u043e \u043c\u043e\u0435 \u0443\u0434\u0438\u0432\u043b\u0435\u043d\u0438\u0435, \u043a\u043e\u0433\u0434\u0430 \u043f\u043e\u0438\u0441\u043a \u043f\u043e\u043a\u0430\u0437\u0430\u043b \u043c\u043d\u0435 FragmentManager.FragmentLifecycleCallbacks. \u0421\u0430\u043c\u044b\u0435 \u043d\u0435\u0442\u0435\u0440\u043f\u0435\u043b\u0438\u0432\u044b\u0435 \u0447\u0438\u0442\u0430\u0442\u0435\u043b\u0438 \u043f\u0438\u0441\u0430\u043b\u0438 \u043f\u0440\u043e \u044d\u0442\u043e \u0432 \u043a\u043e\u043c\u043c\u0435\u043d\u0442\u0430\u0440\u0438\u044f\u0445, \u043f\u043e\u044d\u0442\u043e\u043c\u0443 \u0432\u043e\u0442 \u043f\u0440\u043e\u0434\u043e\u043b\u0436\u0435\u043d\u0438\u0435 \u0432\u0441\u0435\u0439 \u044d\u0442\u043e\u0439 \u0438\u0441\u0442\u043e\u0440\u0438\u0438. \u0421\u043a\u043e\u0440\u0435\u0435 \u043f\u043e\u0434 \u043a\u0430\u0442!<\/p>\n<p>  <\/p>\n<p><img decoding=\"async\" src=\"https:\/\/habrastorage.org\/webt\/py\/v_\/s4\/pyv_s4vjq97ehajg713vrm_oodm.jpeg\"><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-300278","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/300278","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=300278"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/300278\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=300278"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=300278"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=300278"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}