InputMethodService was introduced to InputMethodService while learning about input methods. In order to record my learning process of Google’s open source InputMethodService, I decided to write a column about input methods, to deepen my understanding of input methods, but also for netizens to do relevant reference, there are mistakes welcome to correct. First of all, I want to introduce some knowledge about the input method.

InputMethodService

To implement an input method application, it is essential to understand the life cycle of InputMethodService. By understanding the life cycle of InputMethodService, you can know when to draw the Input View and candidate fields. Let’s take a look at the process through an input lifecycle flowchart.

Let’s take a closer look at the characteristics of each lifecycle

onCreate()

This method is first called when an input method is created. Because it is resident in the process, this method is called only once before OnDestory is available

onInitializeInterface()

Called immediately after the onCreate () method, for interface initialization and when configuration information changes during service execution (horizontal/vertical conversions, etc., trigger the onConfigurationChanged() method)

onBindInput

This method is called when a new client is bound to an input method. When the input method is started for the first time, the onStartInput method is immediately called to get the edit box data. Otherwise, the onFinishInput method is called first, followed by onStartInput

onStartInput

This method is called after onBindInput(). It is used to process the input session initiated by the client. The input method can obtain the information of the corresponding edit box, which is used to decide what type of keyboard to display. It is called frequently during the input method cycle.

onCreateInputView()

This lifecycle is used to initialize classes and variables associated with the Input Area.

onCreateCandidateView()

This lifecycle is primarily used to initialize classes and variables associated with the candidate Area.

onCreateExtractTextView

Called in full-screen mode of input method, used to create and return area view for displaying (Extracted text) text information, the returned view must contain ExtractEditText and ID value is inputExtractEditText. In landscape mode by default, The input method is full-screen.

onConfigureWindow()

It is usually called when the window changes, such as when the input view is acquired and when the input view is lost.

onStartInputView()

This method is used to create and return a hierarchical view of the input area. This method is called only once (the first time the input area is displayed). This method can return null, where there is no input area. The default method implementation of InputMethodService returns a null value. To change an input field View that has been created, we can call setInputView(View). To control when to display an input View, we can implement onEvaluateInputViewShown. The onEvaluateInputViewShown method is called in the updateInputViewShown method to determine whether the input region should be displayed.

onStartInputView()

Input view is showing and have access to edit box input focus when the callback, the method always in onStartInput onStartInputView method, onConfigureWindow () method is invoked. In general, common Settings can be done in the onStartInput method, and view-related Settings can be done in the onStartInputView method. Developers should ensure that the onCreateInputView method is called before the onStartInputView method is called.

onStartCandidiateView()

To call back the method while the candidate view is being displayed, you must ensure that the resources for the candidate area have been initialized. In general, common Settings can be done in the onStartInput method and view-related Settings can be done in the onStartCandidatesView method. The developer should ensure that the onCreateCandidatesView method is called before the method is called.

onWindowShown()

Called after the onStartinputView method, which means that the entire input method is visible.

hideWindow()

This method is called when the input method window loses focus.

onWindowHidden()

This method is called when a view has been converted from visible to invisible. Usually used with onWindowshow.

onFinishCandidatesView()

This method is called when the candidate view is about to be hidden or switched to another edit box. FinishingInput is true and the onFinishInput method is then called.

onFinishInputView()

This method is called when the candidate view is about to be hidden or switched to another edit box. FinishingInput is true and the onFinishInput method is then called.

onFinishInput()

OnFinishInputView onFinishInputView onFinishInputView onStartInput onFinishInputView onFinishInputView onFinishInputView

onUnbindInput()

This method is called when contact is lost with the bound client.

onDestory()

Called when the input method service ends. Call only once. Do a good job of freeing resources in the method again

Understanding the input method life cycle helps us to initialize the corresponding logic at the right time, which is an indispensable part of building an input method program. In the later stage, the communication and association between IMS and IMM will be explored more deeply from the perspective of source code. Please look forward to it.