What is application layout and how can it be done?
7 minute(s) read
Published on: Apr 29, 2021
Updated on: Dec 14, 2021
This word is a general word which means that multiple views are used.
Pane means multiple views. Of course, composite ideas that consist of several opinions are displayed according to the size and capacity of the screen. If there is not enough space to express multiple panels or composite views, only one thought or conference is displayed. In this part of the tutorial, we want to tell you about Fragments in the Android operating system, and it is recommended that you follow this article.
What is a Fragment?
Fragments are independent components that any activity can use. It should be noted that it encapsulates all the necessary capabilities, so it is an encapsulation that is easy to use in exercise and layout.
Although the fragments are inside the text of the activities, they also have a separate life-cycle and user interface. Of course, it is possible that Hubs are below the user interface, and it is possible to define them without the user interface. Examples of fragments without a user interface include a headless it.
How to access Context
It should be noted that fragments are not below the Context class. Because fragments are not under the Context class, it is best to use the getActivity () method to access parent activities.
Advantages of using this
Using Fragment makes it possible to reuse components in layouts. In other words, fragments make it possible to reuse components in the layout. For example, you can define single-frame layouts for phones and multi-frame layouts for smartphones and tablets. Fragments on Android smartphones can be defined to support both landscape and portrait orientations.
This is why it can be easily added or removed dynamically to the activity.
An example for this section is that all the activity items in the tablet device are on one page, and to be able to access the activities, we can select the desired item from the same page. But to access things on mobile smart devices, we must choose the detail screen to make the page wider and select the object we want.
For example, suppose you have two of these: central and detail. Also, consider that you have two activities, one of which is foremost and the other is detailed. It is noteworthy that the main activity consists of two fragments in the tablet smartphone, while the smartphone devices have the main Fragment in the activity.
How to support screens of different sizes using fragments?
To do this, you can define fragments of each activity inside the layout file, which is called static definition file definition. If we define them when they are executed, they will be called dynamic definitions.
To display different of these inside the screens based on the available spaces, you must use the activity that shows two fragments on smartphones and tablets.
On smartphones, use different activities for each of these as a host. For example, if you look at a tablet smart device, you will see that its user interface uses two fragments in one activity. It is recommended that you use the same activity for smartphones, but you should note that you should consider an alternative design that can accommodate a fragment.
If the detailed Fragment already exists, the main activity will instruct the Fragment to perform the update operation. Still, if the apparent Fragment does not already exist, careful training will take action to execute the main activity, will do.
Dynamic implementation is much more flexible, but setting it up will be a little more complicated.
How to define these
To define and use a new fragment, you must expand the android.app. Fragment class or one of the classes is a subclass of this class. Subclasses include ListFragment, Dialog Fragment, Preference Fragment, or WebViewFragment.
How to develop and expand subclasses to create a new fragment is as follows:
By entering the above code, you can either expand the subclasses or create a new fragment and use it.
How to communicate between a program made with a fragment
It should be noted that to reuse the Fragment, and you should not act directly and use them now. Also, when communicating between the program and the fragments, you should not do so instantly. The connection between the program and the Fragment must be made through the host activity.
To do this, the Fragment must define an interface called inner type. Also, the activity that uses the said Fragment must eventually use this interface—using the OnAttach () method, whether the training is implemented correctly by the Fragment or not.
In the following example, imagine a fragment that you are supposed to pass values to the parent activity, such as:
It should be noted that the Fragment lifetime depends on the lifetime of the host activity. If the Fragment stops, so will the activities. Similarly, if any fragments are destroyed, then activity will also be destroyed.
OnAttach () method:
This method is used to attach a fragment instance to an activity instance. Of course, it should be noted that the Fragment and activity are not wholly initialized. Still, a reference to the movement of this method is used to use the said Fragment to continue and complete the initialization process.
OnCreate () method:
This method can be used to create the necessary Fragment.
OnCreateView () method:
fragment creates the view/view hierarchy. The required view/view hierarchy interface is designed in this method. You can inflate the outline inside this method by calling the inflate () method from the inflator object. Remember that you cannot communicate with the activity and interact with it inside this method because, in this section, the training is not fully initialized.
OnActivityCreated () method:
This method is called after the onCreateView () method and when the host activity occurs. The view can be accessed using the findViewById () method in this method.
OnStart () method:
This method is called after the Fragment appears.
OnResume () method:
Fragment is activated using this method.
On Pause () method:
In this method, a fragment is visible but not active.
On Stop () method:
Using this method, the Fragment stops and cannot be viewed.
OnDestroyView () method:
This view method, which is related to the desired Fragment, is eliminated.
On Destroy () method:
This method may not be called at all; the probability of calling this method is very low.
Fragments for activity
To do this, you can statically add a fragment to the XML layout
The code below shows that the android : name attribute refers to the corresponding class.
You can make the most of this method and scenario if you have separate or different static layout files for different device configurations.
Manage fragments dynamically
The getFragmentManager () method can be used to access the fragmentmanager class in the activity. This class allows you to delete the desired fragments in terms of layoutactivity or add a new fragment to them. It is worth noting that you can even replace them with new fragments.
Create a FrameLayout placeholder inside the layout file to modify the fragments. Like the following:
By entering the above code, the new Fragment replaces the old Fragment. This tutorial will teach you how to work with the fragment class to create multiple-page layouts in applications built for the
Android operating system. After following this article, you can easily apply the settings provided for the necessary formatting in applications.DotNek Android development services