-1

We called requestFocus to focus on EditText automatically but sometimes it works or it doen't work. so we added the runnable to UI thread to focus on it.

view.post(new Runnable() {
      @Override
      public void run() {
        view.requestFocus();
        InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, InputMethodManager.SHOW_FORCED);
      }
    });

Finally we know it works well but I'm really curious of the reason. Why? Is there anyone who knows it?

kimkevin
  • 2,202
  • 1
  • 26
  • 48
  • UI thread is main thread in Android, whats yiur confusion? – Oleg Bogdanov Jan 12 '17 at 07:31
  • 1
    Main thread and the UI thread are the same thing i.e they are the same entity. So your assumption is wrong and there might be other issues (specifically focus ownership between parent and children elements). I will suggest you have a look at your view hierarchy and make sure that it's not some view else which is consuming the focus. – Dibzmania Jan 12 '17 at 07:32
  • 1
    @OlegBogdanov **Main thread is not always UI thread**. Read the docs. – Enzokie Jan 12 '17 at 07:36
  • 1
    cc @Dibzmania **Main thread is not always UI thread** – Enzokie Jan 12 '17 at 07:37
  • @Enzokie proof? – Oleg Bogdanov Jan 12 '17 at 07:37
  • @OlegBogdanov sure : `However, it's possible for a UI thread to be different from the main thread in the case of system apps with multiple views on different threads.` [source](https://developer.android.com/studio/write/annotations.html). – Enzokie Jan 12 '17 at 07:39
  • 1
    @Enzokie - Please read this from Android Official documentation - _When an application is launched, the system creates a thread of execution for the application, called "main." This thread is very important because it is in charge of dispatching events to the appropriate user interface widgets, including drawing events. It is also the thread in which your application interacts with components from the Android UI toolkit (components from the android.widget and android.view packages). As such, the main thread is also sometimes called the UI thread._ – Dibzmania Jan 12 '17 at 07:40
  • https://developer.android.com/guide/components/processes-and-threads.html#Threads – Dibzmania Jan 12 '17 at 07:41
  • 1
    @Dibzmania If an app has no UI does it necessarily to have a UI thread? For example does an app which only has service, does the service runs on UI thread? In your reference it says As such, **the main thread is also sometimes called the UI thread** But what you said is `Main thread and the UI thread are the same thing`. – Enzokie Jan 12 '17 at 07:42
  • @Enzokie, it would still be main and it won't be appropriate to call it UI, but it completely not related to yiur saying that UI and main thread are not always the same - if there's any UI , they are – Oleg Bogdanov Jan 12 '17 at 07:45
  • 1
    Did I said Main thread is UI thread? where? – Enzokie Jan 12 '17 at 07:49
  • @Dibzmania Thank you ~ I think I need to check which view has the focus in my views. I got it. I will try it. – kimkevin Jan 12 '17 at 08:00
  • 1
    @Enzokie What I meant was that if the application has a UI thread then it's nothing but the main thread. Which in the context of the question asked was correct to point out. – Dibzmania Jan 12 '17 at 08:01
  • 1
    @Dibzmania Just to be clear, I am not against to anything what you said except that part that you said `Main thread and the UI thread are the same thing` because that statement are not interchangeable, just like All Cats are animals but not all animals are Cats (*Well some cats are aliens*). Also I just forgot to add smiley in my comments. PEACE! – Enzokie Jan 12 '17 at 08:04

1 Answers1

2

It's too early to call requestFocus() at inflation time, try to call something like:

button.post(new Runnable(){
     @Override 
     public void run(){
        inflatedView.requestFocus();
     } });

which will schedule this call to the main queue (so it will be called in the future), after the main thread finish its job here.

https://stackoverflow.com/a/21181502/7267105

Community
  • 1
  • 1
Sadiq Md Asif
  • 882
  • 6
  • 18