(Xamarin.Forms) Android API 28 or later, I want to focus on the app when the app starts.

3-hand-gum 20 Reputation points
2023-02-09T08:12:34.72+00:00

What we want to achieve.

In Xamarin.Forms since Visual Studio 2017 (API28), the entire app does not get focus when launching the app. DispatchKeyEvent() function does not take the first ACTION=KEYDOWN, only ACTION=KEYUP.

Prior to API27, the entire app would get focus when the app was launched, so both ACTION=KEYDOWN and ACTION=KEYUP could be obtained.

Due to this difference in behavior, we are having trouble with our previous apps not working in the same way.

Q. Is there any way to give focus to the entire app at app startup even after API28?

Assumptions.

We are developing an Android app in Xamarin.Forms.

We would like to port an old application developed in Visual Studio 2015 (API 27 or lower) to Visual Studio 2017 or later (API 28 or higher).

The target device is an Android terminal with a hardware keyboard (known as a handy terminal).

The app has an initial startup screen with no Entry, Button, etc., so that the user presses the number or arrow keys with the hardware keys on the handy terminal, and the process proceeds by getting the input event key with DispatchKeyEvent().

Comparison by API.

For easy comparison, I created and tested a blank Xamarin.Forms app in Visual Studio 2017.

The hardware keyboard part works the same with an external Bluetooth keyboard, etc.

(API27)

When built with API27 (Android 8.1), the entire app (StackLayout section) gets focus when the app is launched, and when the hardware keyboard is pressed, key events are taken for both action=ACTION_DOWN/action=ACTION_UP.

(when the app is launched.)

API27 app launch

(API28)

If I build with API 28 (Android 9) or later, the entire app (StackLayout area) does not get focus when the app is launched and you cannot get key events with action=ACTION_DOWN when the hardware keyboard is pressed. only can be retrieved.

It is an image that the entire application receives focus with ACTION_DOWN and only ACTION_UP can be acquired by the application.

If I build with API 28 (Android 9) or later, the entire app (StackLayout area) does not get focus when the app is launched and you cannot get key events with action=ACTION_DOWN when the hardware keyboard is pressed. only can be retrieved.

(when the app is launched.)

イメージ説明

(When D-pad down is pressed)

イメージ説明

I have read the Android development documentation and other documentation, but there were no changes regarding focus in API 28 (Android 9). Earlier API26 (Andorid 8.0) described a change in the focus specification, but it does not match the phenomenon.

Can anyone help me with a solution?

Best regards.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,297 questions
{count} votes

Accepted answer
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 36,231 Reputation points Microsoft Vendor
    2023-02-10T07:14:28.83+00:00

    Hello,

    the entire app does not get focus when launching the app.

    The cause of this issue should be related to Android API 28+ no longer automatically assigning focus.

    Additionally, activities no longer implicitly assign initial focus in touch-mode. Instead, it is up to you to explicitly request initial focus, if desired.

    Please refer to View focus to get more details.

    On Xamarin, you could manually set the focus by including the following code in the page's code-behind:

    protected async override void OnAppearing()
    {
        base.OnAppearing();
        await Task.Run(async () =>
        {
            await Task.Delay(100);
            Device.BeginInvokeOnMainThread(async () =>
            {
                your_view.Focus();
            });
        });
    }
    

    Best Regards,

    Alec Liu.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful