Pro Android with Kotlin
Download 5.42 Mb. Pdf ko'rish
|
@de android telegram Pro Android with Kotlin Developing Modern Mobile
167
CHAPTER 8: APIs Next we let this activity receive the search request. This is done inside its onCreate() callback as follows: override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_searchable) // This is the standard way a system search dialog // or the search widget communicates the query // string: if (Intent.ACTION_SEARCH == intent.action) { val query = intent.getStringExtra(SearchManager.QUERY) doMySearch(query) } // More initialization if necessary... } The doMySearch() function is supposed to perform the search and present the result inside the SearchableActivity. The way this is done is totally up to the application; it could be a database search or a search using a content provider or anything you want. The Search Dialog For any activity to open the system’s search dialog and have it pass the query entered there to the SearchableActivity, you write the following in AndroidManifest.xml: android:value= ".SearchableActivity" /> This example allows for MainActivity to open the system’s search dialog. In fact, you can use any suitable activity from inside your app for that purpose. To open the search dialog inside your searching activity, write the following: onSearchRequested() 168 CHAPTER 8: APIs Figure 8-7 shows a dialog-based search flow. Note Usually directly executing obvious callback function starting with on... has a bad smell. You usually do that for half-legal shortcuts. The reason why we have to do it here is that your device might have a dedicated search button. In this case, onSearchRequested() gets called from the system, and it is a real callback method. Because such a button is optional, it is, however, necessary to always provide a search initiator from inside your app. Figure 8-7. A dialog-based search flow The Search Widget Instead of opening the system search dialog, you can place a your UI. While in principle you can place it wherever you like, it is recommended you put it in the action bar. For this aim, provided you have set up an action bar and defined a menu there, inside the menu XKML definition you write the following: "http://schemas.android.com/apk/res/android" xmlns:app= "http://schemas.android.com/apk/res-auto"> app:showAsAction="never"/> |
Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©fayllar.org 2024
ma'muriyatiga murojaat qiling
ma'muriyatiga murojaat qiling