Method for realizing focus memory by Android equipment
By customizing the parent control to monitor focus changes and using global variables to save focus information, the problem of missing focus memory function of Android TV devices is solved, efficient focus memory is achieved, and development efficiency is improved.
Patent Information
- Application Number
- CN202411182828.3
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-27
- Publication Date
- 2024-12-10
AI Technical Summary
Android TV devices lack focus memory function, resulting in focus returning to the default state after the interface content is refreshed or switched. The existing solutions are inefficient and error-prone.
Customize the parent control to add global focus change monitoring of view tree, save focus information through global variables, and adjust the focus acquisition status of the parent control when the focus changes to ensure focus memory.
It improves the efficiency of the implementation of the focus memory function, the solution is simple and unified, and significantly improves the development efficiency.
Smart Images

Figure CN119110112A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the field of focus processing, and in particular to a method for implementing focus memory on an Android device. Background Art
[0002] On Android TV devices, users operate TV applications by controlling the focus of the TV interface through the remote control. However, since Android does not natively provide a focus memory function, when the interface content is refreshed or switched, the focus of the interface will return to the default state, which is inconvenient for users to operate. The current solution is to set ID values for all focus controls in the interface and achieve the focus memory effect by recording and transmitting the ID values. However, this method requires adding ID values to all focus controls, transmitting and recording them, which is very inefficient and prone to omissions and errors in actual development. Summary of the invention
[0003] In order to improve the efficiency of implementing the focus memory function, the present application provides a method for implementing focus memory in an Android device.
[0004] The technical solution adopted by the present invention to solve the above problems is:
[0005] Methods for implementing focus memory in Android devices include:
[0006] Step 1. Customize the parent control and add a view tree global focus change listener;
[0007] Step 2: Set a global variable to save the current focus information. If the focus changes inside the parent control, save the focus information in time.
[0008] Step 3: If the focus moves out of the current parent control, set the parent control to not allow the child control to get the focus, and the parent control will get the focus next time;
[0009] Step 4: When the parent control regains the focus, set the parent control to obtain the focus after the child control during the secondary focus allocation, and restore the focus information through the global variable.
[0010] Furthermore, the step 1 adds a view tree global focus change monitor when the parent control is initialized.
[0011] Furthermore, the method for determining whether the focus has moved out of the parent control in step 2 is: if the last focus belonged to the child control of the current parent control and the current parent control has no focus, it is determined that the current focus has moved out of the parent control.
[0012] Furthermore, the parent control is ViewGroup and its subclasses.
[0013] Compared with the prior art, the present invention has the following beneficial effects: by setting a global focus event listener in the parent control to determine whether the focus has moved out of the parent control, and using a global variable to save the focus information to achieve focus restoration, the solution has high uniformity, is simple to implement, and can greatly improve development efficiency. BRIEF DESCRIPTION OF THE DRAWINGS
[0014] Figure 1 Flowchart of a method for implementing focus memory for Android devices. DETAILED DESCRIPTION
[0015] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not used to limit the present invention.
[0016] like Figure 1 As shown, the method for implementing focus memory in an Android device includes:
[0017] Step 1. Customize the parent control and add a view tree global focus change listener.
[0018] The parent control is ViewGroup and its subclasses in the Android system. This embodiment takes the parent control FrameLayout as an example, creates a custom subclass of the FrameLayout class as the parent control, and calls the getViewTreeObserver().addOnGlobalFocusChangeListener(OnGlobalFocusChangeListener listener) method in the parent control initialization process to register the focus change monitor of the view tree.
[0019] Step 2: Set a global variable to save the current focus information. If the focus changes within the parent control, save the focus information in time.
[0020] Override the requestChildFocus(child:View,focused:View) method of the parent control. When the child control inside the parent control gains focus, this method will be called recursively to pass the focus to the upper parent control. In the requestChildFocus(child:View,focused:View) method, the global variable mFocusChild can be assigned the focused object to save the record of the current focus control of the parent control.
[0021] Step 3: If the focus moves out of the current parent control, set the parent control to not allow the child control to obtain the focus, and the parent control will obtain the focus next time.
[0022] Based on step 1, you need to implement an OnGlobalFocusChangeListener instance to register a focus change listener. Make a judgment in the onGlobalFocusChanged(View oldFocus, View newFocus) function of the OnGlobalFocusChangeListener instance. If oldFocus is not null and oldFocus belongs to the child control of the current parent control, and the current parent control has no focus, it is determined that the current focus has moved out of the parent control, and the parent control is set to the FOCUS_BLOCK_DESCENDANTS state.
[0023] Step 4: When the parent control regains the focus, set the parent control to obtain the focus after the child control during the secondary focus allocation, and restore the focus information through the global variable.
[0024] When the parent control is about to gain focus, the requestFocus(direction:Int,previouslyFocusedRect:Rect) method of the parent control is triggered. Rewrite the requestFocus(direction:Int,previouslyFocusedRect:Rect) method of the parent control and set the parent control to FOCUS_AFTER_DESCENDANTS state so that the child control it remembers can gain focus. If the global variable mFocusChild has a saved object, call the requestFocus() method of the mFocusChild object instance to gain focus and restore focus.
Claims
1. How to implement focus memory on Android devices. It is characterized in that include: Step 1. Customize the parent control and add a view tree global focus change listener; Step 2: Set a global variable to save the current focus information. If the focus changes inside the parent control, save the focus information in time. Step 3: If the focus moves out of the current parent control, set the parent control to not allow the child control to get the focus, and the parent control will get the focus next time; Step 4: When the parent control regains the focus, set the parent control to obtain the focus after the child control during the secondary focus allocation, and restore the focus information through the global variable.
2. The method for implementing focus memory in an Android device according to claim 1, It is characterized in that The step 1 adds a view tree global focus change listener when the parent control is initialized.
3. The method for implementing focus memory in an Android device according to claim 1, It is characterized in that The method for determining whether the focus has moved out of the parent control in step 2 is as follows: if the last focus belonged to the child control of the current parent control and the current parent control has no focus, it is determined that the current focus has moved out of the parent control.
4. The method for implementing focus memory in an Android device according to claim 1, It is characterized in that The parent control is ViewGroup and its subclasses.
