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
- standard --|--
- singleTop --|--
- singleTask --||--
- singleInstance --||--
- singleInstancePerTask --||--
- Creates and launches new instance of the activity every time.
- 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.
-
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
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.
-
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
-
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.
-
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.
Intro - Committing fragment transaction via commit() after onSaveInstanceState() will throw IllegalStateException.
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.
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.
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.
Synchronous Like commitNow but allows the commit to be executed after an activity's state is saved. Remaining is similar to commitAllowinfStateLoss()
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
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
Context value is the activity object context which is printed in onAttach
User navigating away from screen which contained fragment and then returning back (Single Fragment) (With or without backstack same result)
User navigating away from screen which contained fragment and then returning back (Mutiple Fragment) (With or without backstack same result)
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.
Note - See how onDestroyView() method of Fragment1 is also called.
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.
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.
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.
Very Very Important -
- When there are multiple fragments added on a screen then if device home button is pressed
- or the user returns to the screen from recents tab of device
- or if user goes to new activity
- 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.
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
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.
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.
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.
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.
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.