fragments, PRLAB
A Fragment is a piece of an activity which enable more modular activity design. It will not be wrong if we say, a fragment is a kind of sub-activity.
A fragment has its own layout and its own behaviour with its own life cycle callbacks.
You can add or remove fragments in an activity while the activity is running.
. You can combine multiple fragments in a single activity to build a multi-pane UI.
A fragment can be used in multiple activities.
. Fragment life cycle is closely related to the life cycle of its host activity which means when the activity is paused, all the fragments available in the activity will also be stopped.
A fragment can implement a behaviour that has no user interface component.
Fragments were added to the Android API in Honeycomb version of Android which API version 11.
The application can embed two fragments in Activity A, when running on a tablet-sized device. However, on a handset-sized screen, there's not enough room for both fragments, so Activity A includes only the fragment for the list of articles, and when the user selects an article, it starts Activity B, which includes the second fragment to read the article.
Here is the list of methods which you can to override in your fragment class -
onAttach()The fragment instance is associated with an activity instance. The fragment and the activity is not fully initialized. Typically you get in this method a reference to the activity which uses the fragment for further initialization work.
onCreate() The system calls this method when creating the fragment. You should initialize essential components of the fragment that you want to retain when the
fragment is paused or stopped, then resumed.
onCreateView() The system calls this callback when it's time for the fragment to draw its user interface for the first time. to draw a UUI for your fragment, you must return a View component from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a Ul.
PRLAB:
FILE NAME: MAINACTIVITY.KT
package com.example.myfirstkotlinapp
import android.os.Bundle
import android.widget.TextView
import android.widget.Toast
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//accessing our textview from layout val textView = findViewById<TextView>(R.id.text_view_id) as TextView textView?.setOnClickListener{ Toast.makeText(this@MainActivity, R.string.text_on_click, Toast.LENGTH_LONG).show()}
}
FILE NAME: MAINACTIVITY.KT
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--EditText with id editText-->
<TextView
android:id="@+id/text_view_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="@string/text_view"
android:textColor="#008000"
android:textSize="40dp"
android:textStyle="bold"/>
</LinearLayout>
FILE NAME: strings.xml
<string name="app_name">myfirstkotlinapp</string>
<string name="text_view">PRLAB AITS</string>
<string name="text_on_click">FOR AIDS LAB</string>
</resources>
Comments
Post a Comment