Pro Android with Kotlin


Download 5.42 Mb.
Pdf ko'rish
bet127/223
Sana04.11.2023
Hajmi5.42 Mb.
#1746807
1   ...   123   124   125   126   127   128   129   130   ...   223
Bog'liq
@de android telegram Pro Android with Kotlin Developing Modern Mobile

143
CHAPTER 8: APIs
Here are a few notes:

@SuppressLint(”StaticFieldLeak”) will suppress the warning about 
possible memory leakage given inside Android Studio. The loader 
lifecycle is governed by the Loader framework, and makeLoader() will 
return a reusable loader, so the possible leak is mitigated. Instead, 
by moving it to a static field, which in Kotlin means providing it as an 
object, is not easy to do here since we need a reference to the activity 
for constructing the AsycTaskLoader.

We provide for the Boolean initLoaded field to make sure the loading 
will be actually started by calling forceLoad() the first time.

By design, the loadInBackground() method gets called in a background 
thread. This is where the loading actually happens. In the example we 
just count from 0 to 9. In a real-world scenario, you will of course do 
more interesting things here.

To help the framework maintain a correct loader state, 
inside loadInBackground() you should regularly check 
isLoadInBackgroundCanceled and act accordingly. In the example, we 
throw an OperationCanceledException, which will not break your app but 
will be handled by the Loader framework. In fact, it will be transported up 
and eventually call the onLoaderReset() callback method.

The method onStartLoading() gets called by the framework; you don’t 
have to do that yourself.
All that is missing now is to start and maybe stop the loading. If you use two buttons for that 
in the UI, the corresponding methods read as follows:
fun go(view: View) {
loaderManager.initLoader(LOADER_ID,null,this)
}
fun dismiss(view: View) {
loaderManager.getLoader(LOADER_ID)?.
cancelLoad()
loaderManager.destroyLoader(LOADER_ID)
}
The cancelLoad() method is necessary to tell the loader to cancel its loading operation, and 
the destroyLoader() method will unregister the loader from the Loader framework.
Notifications
A notification is a message an app can present to the user outside its normal GUI flow. 
Notifications show up in a special region of the screen, most prominently inside the status 
bar and notification drawer on top of a screen, in special dialogs, on the lock screen, on 
a paired Android Wear device, or on an app icon badge. See Figures 
8-1
and 
8-2
for a 
smartphone example. There you can see the notification icon and the notification content 
after the user expands the notification drawer.



Download 5.42 Mb.

Do'stlaringiz bilan baham:
1   ...   123   124   125   126   127   128   129   130   ...   223




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling