Coder Social home page Coder Social logo

mahmood199 / dissecting-lifecycle-in-android Goto Github PK

View Code? Open in Web Editor NEW
3.0 1.0 0.0 133 KB

A repository dedicated to logging and understanding the lifecycle of activities, fragments and both of them combined. This can be referred for revision purpose before interviews. README contains good QnA for interviews

Kotlin 100.00%

dissecting-lifecycle-in-android's Introduction

Dissecting-LifeCycle-In-Android

A repository dedicated to logging and understanding the lifecycle of activities, fragments and both of them combined. This can be referred for revision purpose before interviews. README contains good QnA for interviews

Lifecycle callbacks of activities when launching new activity from another

image

and when back button is pressed from new activity

image

Lifecycle callbacks of activity when screen is rotated

image

When device home button is pressed

image

When User returns back to app from recents tab

image

Task Modes in Android

  1. standard --|--
  2. singleTop --|--
  3. singleTask --||--
  4. singleInstance --||--
  5. singleInstancePerTask --||--

1. standard

  • Creates and launches new instance of the activity every time.

2. singleTop

  • If an instance of the activity already exists at the top of the current task and the system routes the intent to this activity, no new instance will be created because it will fire off an onNewIntent() method instead of creating a new object. If there is no instance of this at the top then it relaunches the activity.

TaskAffinity - Preference of the activity regarding with which task it will associate itself. Basically the task in which they will be launched. By default all activities have the same affinity. Hence they belong to the same task. This affinity is package affinity, basically package name. To change taskAffinity for an activity we specify the android:taskAffinity="some random name". This enables us to switch between different tasks of the same application from recents tab of device.

3. singleTask

  1. with taskAffinity - if we launch our activity with a task affinity then it will open up in new task. Any activity launched by this activity will be kept in its task as it shares the affinity of the root activity image In a nutshell, it means that singleTask activity will always be at the root of task. there will be only one instance of activity across tasks. Also in the recents tab, we will see 2 tasks of same application.

  2. without taskAffinity - if there is no instance of activity in the task then a new instance is created and launched. But if its there then it will simply intercept the intent via onNewIntentn() method. If there are some actvities stacked on top of singleTask activity and it is launched again then all above activities gets popped off the task. Also if there's no affinity then it isn't necessary that it will be present at the root of the task

4. singleInstance

  1. with taskAffinity - new activity is launched in a new task. if new activities are launched from this activity then those activities will be launched in original task. Also this activity will be the only activity instance in the new task Also in the recents tab, we will see 2 tasks of same application.

  2. without taskAffinity - new activity is launched in a new task. if new activities are launched from this activity then those activities will be launched in original task. Also this activity will be the only activity instance in the new task. Also in the recents tab, we will see only 1 task of same application.

Fragment Transaction actions

Intro - Committing fragment transaction via commit() after onSaveInstanceState() will throw IllegalStateException.

commit() -

Asynchronous Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready. A transaction can only be committed with this method prior to its containing activity saving its state. If the commit is attempted after that point, an exception will be thrown. This is because the state after the commit can be lost if the activity needs to be restored from its state.

commitNow()

Synchronous cant add fragment to backstack with this method. However it immediately schedules the work by placing it at the front of the queue. Transactions committed in this way may not be added to the FragmentManager's back stack,as doing so would break other expected ordering guarantees for other asynchronously committed transactions. cant add fragment to backstack with this method. However it immediately schedules the work by placing it at the front of the queue.

commitAllowingStateLoss()

Asynchronous Schedules a commit of this transaction. The commit does not happen immediately; it will be scheduled as work on the main thread to be done the next time that thread is ready. A transaction can be committed with this method after its containing activity saving its state. If the commit is attempted after that point it wont be saved to fragment manager so state would be lost. So whether it is adding/removing/replacing fragment it wont happen take place.

commitNowAllowingStateLoss()

Synchronous Like commitNow but allows the commit to be executed after an activity's state is saved. Remaining is similar to commitAllowinfStateLoss()

Difference between commitNow() vs commit() followed by executePendingTransactions()

Calling commitNow is preferable to calling commit() followed by FragmentManager.executePendingTransactions() as the latter will have the side effect of attempting to commit all currently pending transactions

Fragments

Note - Create new fragment transaction every time before adding/replacing fragment otherwise while committing on an already committed transaction will throw commint Already called on transaction exception. This is because each transaction can be committed only once

When fragment is added due to any action

Context value is the activity object context which is printed in onAttach image

When more fragments are added on top of existing fragment.

Note how the lifecycle of previously added fragment is not affected when new fragments are ADDED

image

User navigating away from screen which contained fragment and then returning back (Single Fragment) (With or without backstack same result)

image

User navigating away from screen which contained fragment and then returning back (Mutiple Fragment) (With or without backstack same result)

image

Fragment popped from backstack

image

Difference between add and replace transactions https://stackoverflow.com/questions/18634207/difference-between-add-replace-and-addtobackstack

First fragment added without backstack. Second fragment added via replace method but without backstack

Note - See how onDestroyView(), onDestroy() and OnDetach() method of Fragment1 is also called. image

First fragment added without backstack. Second fragment added via replace method with backstack

Note - See how onDestroyView() method of Fragment1 is also called. image

When back button is pressed(just from above state)

image

2 fragments(Fragment 1 and Fragment2) added without backstack. Third fragment added via replace method but without backstack

Also Observe the LIFO order pausing of previously added fragments. image

2 fragments(Fragment 1 and Fragment2) added without backstack. Third fragment added via replace method with backstack

Note - See how onDestroyView() method of Fragment1 is also called. Also Observe the LIFO order pausing of previously added fragments. image

When back button is pressed(just from above state)

Note - Also Observe the FIFO order resuming of previously added fragments. understand that fragment 1 was added before fragment 2. and then replace with backstack with 3rd fragment and then back press on third fragment. image

Very Very Important -

  1. When there are multiple fragments added on a screen then if device home button is pressed
  2. or the user returns to the screen from recents tab of device
  3. or if user goes to new activity
  4. or returns back from another activity ............. Then the fragment lifecycle will be
    Then the framgent lifecycle callback methods are called in FIFO order that the order in which they were added on screen. Example - Fragment1 added , Fragment2 added, Fragment3 added then their lifecycle methods will be called IN THAT ORDER.

Logs for point 1 image

Logs for point 2 image

Logs for point 3 image

Logs for point 4 image

2 fragments(Fragment 1 and Fragment2) added with backstack. Third fragment added via replace method with or without backstack <--- both will produce same results

Also Observe the LIFO order pausing of previously added fragments. And also that only the views were destroyed of previous fragments no fragments instance. pressing back button on third fragment will cause LIFO order callbacks for fragment 1 and 2 image

Back pressed from above state

image

Going to new activity from above state

image

2 fragments (Fragment1 and Fragment2) Added without backstack and another fragment (Fragment3) is added with backstack. Then Fragment1 is added via replace without backstack. So the previously added fragments which didn't add up to backstack just got completely destroyed(view and fragment instance both + detach). And only the vide of fragment 3 is destroyed and fragment1 is brought to the screen.

Note - Notice the LIFO order is still maintained while replacing existing fragments with new one. image

Same as above but 1 fragment(Fragment1) was Added without backstack and another 2 fragments (Fragment2 and Fragment3) are added with backstack.

Note - Notice the LIFO order is still maintained while replacing existing fragments with new one. image

Fragment1 and Fragment2 added with backstack and Fragment3 added without backstack. And replaced by fragment 1 without backstack

Note - Notice the LIFO order is still maintained while replacing existing fragments with new one. image

Fragment1 added with backstack then Fragment2 and Fragment3 added without backstack. And replaced by fragment 1 without backstack

Note - Notice the LIFO order is still maintained while replacing existing fragments with new one. image

When calling replace with backstack. No fragments will be destroyed completely irrespective of whether they were added with or without back stack. Only their view will be destroyed on replacing with new fragment.

Note - Notice the LIFO order is still maintained while replacing existing fragments with new one. image

dissecting-lifecycle-in-android's People

Contributors

mahmood199 avatar mahmoodatsemaai avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar

dissecting-lifecycle-in-android's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.