If you read the earlier Article in which I talked about the BackStack then you might have noticed the following code in the MainActivity layout file.
I mentioned that using a FrameLayout instead of a Fragment element is helpful if we want to replace the current fragment with another one.
In our scenario when a user will click on an Employee then the Employee detail will appear, which means the detail fragment will be replaced with a new instance every time.
You can refer to the Source Code here
When you use an App on your Android Device I am sure you press the back button pretty often. Which in turn takes you to the previous Screen.
This happens because of the Back Stack which maintains a history of places you have visited.
Each visit is a transaction. So the back stack essentially keeps an eye on all the transactions. Using the back stack effectively can lead to to a good user experience.
A transaction can be moving from one Activity to another or from one Fragment to another. So if the user wants to go back to the previous Activity or Fragment
the Back Button will enable that action.
How this concept applies to our App we will discover in the next Article.
Read the Android Documentation to explore this further.
Now that you have two Fragments – a ListFragment and a Detail Fragment, its time for you to call the Detail Fragment from ListFragment.
Let’s understand the Fragment Communication concepts.
Continue reading “Learn how Fragments communicate with each other via Activity”
It’s time for you to start with a new App. We will call this one EmployeeApp
In this Video you will see how to create this App Project. In the coming Tutorial Posts you will learn about Back Stack, Fragment Communication, Fragment Transaction Manager and More.
So get started.
Source Code for this App : https://github.com/ankur-srivastava/EmployeeApp
You have seen how to use ListActivity to display a List of items in an Activity. Similar to that we have a ListFragment which can be used to display a List of items in a Fragment.
The advantages of using a ListFragment are:
- You don’t have to create a Layout XML file. You can if you want.
- You don’t have to create an EventListener.
To learn more please watch the Video.
Source Code available here
Now that you have created an Activity and a Fragment chances are that your Activity needs to talk to the Fragment.
Like your Activity may need to send the Fragment some data, say List ID. And using this ID the Fragment will display details.
To refer to a Fragment you cannot use the findViewById method.
In order to reference a Fragment in an Activity we use FragmentManager.
- Step 1 Use getFragmentManager() which is available in your Activity class to get a reference to FragmentManager.
- Step 2 Use getFragmentManager().findFragmentById() to get reference to the Fragment you need to use.
Follow the Video to learn more. You can view the Source Code here.
By now you should be familiar with the Activity Lifecycle looks like. If not don’t worry in this short video I will recap that also.
The Fragment Lifecycle is quite similar to the Activity Lifecycle. If you observe you will find that the Fragment has five more Lifecycle events.
The difference is at the time of Creation and when the Fragment gets destroyed.