Fragments
- A
Fragment
represents a reusable portion of your app’s UI. - A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events.
- Fragments cannot live on their own–they must be hosted by an activity or another fragment. The fragment’s view hierarchy becomes part of, or attaches to, the host’s view hierarchy.
Fragments | Android Developers
A Fragment represents a
reusable portion of your app’s UI. A fragment defines and manages its own
layout, has its own lifecycle, and can handle its own input events. Fragments
cannot live on their own–they must be hosted by an activity or another
fragment. The fragment’s view hierarchy becomes part…
reusable portion of your app’s UI. A fragment defines and manages its own
layout, has its own lifecycle, and can handle its own input events. Fragments
cannot live on their own–they must be hosted by an activity or another
fragment. The fragment’s view hierarchy becomes part…
Fragment lifecycle
- Each
Fragment
instance has its own lifecycle. - When a user navigates and interacts with your app, your fragments transition through various states in their lifecycle as they are added, removed, and enter or exit the screen.
- To manage lifecycle,
Fragment
implementsLifecycleOwner
, exposing aLifecycle
object that you can access through thegetLifecycle()
method.
Fragment lifecycle | Android Developers
Each Fragment instance has
its own lifecycle. When a user navigates and interacts with your app, your
fragments transition through various states in their lifecycle as they are
added, removed, and enter or exit the screen.
its own lifecycle. When a user navigates and interacts with your app, your
fragments transition through various states in their lifecycle as they are
added, removed, and enter or exit the screen.
Share data between fragments
- Using
ViewModel
objects. T
- These fragments can share a
ViewModel
using their activity scope to handle this communication
TaskListFragment – Start Fragment
/**
* By activityViewModels is a property delegate to get a reference to the ViewModel scoped to
* its Activity. So, our shared ViewModel will survive across Fragment recreation.
*/
private val taskViewModel: TaskViewModel by activityViewModels {
TaskViewModelFactory()
}
Navigation
- Navigation refers to the interactions that allow users to navigate across, into, and back out from the different pieces of content within your app.
- Android Jetpack’s Navigation component helps you implement navigation, from simple button clicks to more complex patterns, such as app bars and the navigation drawer.
- The Navigation component also ensures a consistent and predictable user experience by adhering to an established set of principles.
Navigation | Android Developers
Use the Navigation component in Android Jetpack to implement navigation in your app.
Menu
- Menus are a common user interface component in many types of applications.
- To provide a familiar and consistent user experience, you should use the
Menu
APIs to present user actions and other options in your activities.
Menus | Android Developers
Menus are a common user interface component in many types of applications. To provide a familiar
and consistent user experience, you should use the Menu APIs to present user
actions and other options in your activities.
and consistent user experience, you should use the Menu APIs to present user
actions and other options in your activities.
Widgets
- Useful widgets for the implementation
Date Picker
- Material DateTime Picker tries to offer you the date and time pickers as shown in the Material Design spec, with an easy themable API.
- The library uses the code from the Android frameworks
GitHub – wdullaer/MaterialDateTimePicker: Pick a date or time on Android in style
Pick a date or time on Android in style. Contribute to wdullaer/MaterialDateTimePicker development by creating an account on GitHub.
Spinners
- Spinners provide a quick way to select one value from a set. In the default state, a spinner shows it’s currently selected value.
- Touching the spinner displays a dropdown menu with all other available values, from which the user can select a new one.
Spinners | Android Developers
Spinners provide a quick way to select one value from a set. In the default state, a spinner
shows its currently selected value. Touching the spinner displays a dropdown menu with all other
available values, from which the user can select a new one.
shows its currently selected value. Touching the spinner displays a dropdown menu with all other
available values, from which the user can select a new one.
Logging
- Logging too much information will blow up your Android monitor
- OkHttp’s logging interceptor has four log levels:
NONE
,BASIC
,HEADERS
,BODY
.
ServiceBuilder
object ServiceBuilder {
vargson: Gson = GsonBuilder()
.setDateFormat(BuildConfig.DATE_FORMAT)
.create()
// If you need to check your object ServiceBuilder {
var gson: Gson = GsonBuilder()
.setDateFormat(BuildConfig.DATE_FORMAT)
.create()
// If you need to check your request change the Level
var loggingInterceptor: HttpLoggingInterceptor = HttpLoggingInterceptor().setLevel(
HttpLoggingInterceptor.Level.NONE
)
private val client =
OkHttpClient.Builder().addInterceptor(loggingInterceptor)
.addInterceptor(AuthorizationInterceptor()).build()
private val retrofit = Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL) // change this IP for testing by your actual machine IP
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build()
fun <T> buildService(service: Class<T>): T {
return retrofit.create(service)
}
} change the Level
varloggingInterceptor: HttpLoggingInterceptor = HttpLoggingInterceptor().setLevel(
HttpLoggingInterceptor.Level.NONE
)
private valclient=
OkHttpClient.Builder().addInterceptor(loggingInterceptor)
.addInterceptor(AuthorizationInterceptor()).build()
private valretrofit= Retrofit.Builder()
.baseUrl(BuildConfig.BASE_URL)// change this IP for testing by your actual machine IP
.addConverterFactory(GsonConverterFactory.create(gson))
.client(client)
.build()
fun<T> buildService(service: Class<T>): T {
returnretrofit.create(service)
}
}
Retrofit 2 — Log Requests and Responses
Future Studio provides on-demand learning & wants you to become a better Android (Retrofit, Gson, Glide, Picasso) and Node.js/hapi developer!