2

System services not available to Activities before onCreate()

I have dialog class MyPersonalDialog(mContext: Context) and this dialog contains EditText. I initiate class MyPersonalDialog by parsing context in there. val myPersonalDialog = MyPersonalDialog(this)

and then I chow dialog my calling myPersonalDialog.showMyDialog

this class:

class MyPersonalDialog(mContext: Context){

    fun showMyDialog(){
    val builder = AlertDialog.Builder(context)
    val layoutInflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
    val view = layoutInflater.inflate(R.layout.dialog_edit_list_title, null)
    view.renameListTitle.requestFocus()
    val inputHelper = InputHelper(context)
    inputHelper.showDialogKeyboard()
    builder.setView(view)
    builder.setNegativeButton(R.string.cancel, { dialogInterface: DialogInterface, i: Int ->
        inputHelper.hideKeyboard(activity, view)
    })
    //some other code goes next
}

}

When user presses NegativeButton button hideKeyboard start to works

class InputHelper(val context: Context){
fun hideKeyboard(activity: Activity, view: View) {
        val inputManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS)
}
}

But this error appears: java.lang.IllegalStateException: System services not available to Activities before onCreate()

How can I fix this issue? I do not undestand why does this error appears because MyPersonalDialog class was initiated after onCreate

Solution is found:

class InputHelper(val context: Context){
fun showDialogKeyboard() {
    val inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0)
}

fun hideKeyboard(view: View) {
        val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        inputManager.hideSoftInputFromWindow(view.getWindowToken(),0);
}}
  • can you show where you have written `val myPersonalDialog = MyPersonalDialog(this)`? – Antonio Jun 29 '19 at 14:46
  • class MainActivity : OptionMenuHelper() { override fun onCreate(savedInstanceState: Bundle?) { showRecycler(){ } fun showRecycler(){ //some code and then: val recyclerViewOfLists = RecyclerViewOfLists(cursor, this, MainActivity()) recyclerOfListsTitles.adapter = recyclerViewOfLists } } class RecyclerViewOfLists(var cursor: Cursor, val context: Context, val activity: Activity): RecyclerView.Adapter() { inner class ViewHolderHelper(itemView: View) : RecyclerView.ViewHolder(itemView){ itemView.setOnClickListener { showMyDialog() }}} – Pavel Malinovskij Jun 29 '19 at 15:14
  • Actually inside of onCreate I call RecyclerViewOfLists: RecyclerView.Adapter() and from there I call dialog. – Pavel Malinovskij Jun 29 '19 at 15:16
  • can you edit the question and include main activity in it? – Antonio Jun 29 '19 at 15:16
  • already done :) – Pavel Malinovskij Jun 29 '19 at 15:22
  • you have initialised `InputHelper(this)` before onCreate – Antonio Jun 29 '19 at 15:30
  • can you try initialising it in onCreate? – Antonio Jun 29 '19 at 15:31
  • and there is no part in the code you have given where `MyPersonalDialog` is initialised – Antonio Jun 29 '19 at 15:37
  • just tried to do this but it did not helped, error is same... I suppose it could be something wrong with RecyclerViewOfLists class (I also added it to question), how do you think? – Pavel Malinovskij Jun 29 '19 at 15:38
  • "and there is no part in the code you have given where MyPersonalDialog is initialised" I tried to simplifie to save your time, actualy I call dialog using function makeDealog inside of RecyclerViewOfLists class – Pavel Malinovskij Jun 29 '19 at 15:41
  • is `inputHelper.showDialogKeyboard()` in RecyclerViewOfLists working properly? – Antonio Jun 29 '19 at 15:47
  • Yes, it works, I just added InputHelper class to the end of my question, so you can see it too :) showDialogKeyboard() works, hideKeyboard(activity: Activity, view: View) does not – Pavel Malinovskij Jun 29 '19 at 15:54
  • can you try replacing `fun hideKeyboard(activity: Activity, view: View) { val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager inputManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS) } }` in `InputHelper`? – Antonio Jun 29 '19 at 15:56
  • LogCat tells that first line inside of hideKeyboard function is wrong – Pavel Malinovskij Jun 29 '19 at 15:57
  • what is the error? – Antonio Jun 29 '19 at 15:57
  • very strange things happens, now it works with your pcs of code but every second time, I will make video and add youtube link in 10-15minutes. – Pavel Malinovskij Jun 29 '19 at 16:11

4 Answers4

1

try replacing fun hideKeyboard(activity: Activity, view: View) { val inputManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager inputManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS) } } in InputHelper

Antonio
  • 1,061
  • 5
  • 12
  • this code works but i noticed that it works every second time (as well as showDialogKeyboard() ), you can watch it on video: https://youtu.be/rbT6_gpReSo do you have some idea why could this happen? My porpose to make keyboard shown every time when I press "edit title" and hide it every time when i press cancel – Pavel Malinovskij Jun 29 '19 at 16:20
  • can you try commenting `inputHelper.showDialogKeyboard()` in the makeDealog function? – Antonio Jun 29 '19 at 16:24
  • coz the keyboard will be shown whenever you focus the edittext. you need not do it programmatically – Antonio Jun 29 '19 at 16:25
  • now hideKeyboard(activity: Activity, view: View) works perfectly, it hides keyboard every time when I press cancel :) But keyboard do not appear automaticaly when i press "edit title", I need now to make additional click on EditText to make keyboard shown... It seems to me that showDialogKeyboard function is wrong, because when I commented it function hideKeyboard worked perfectly. How do you think? How could i make keyboard shown automaticaly? video: https://youtu.be/8mGIxPwtncY – Pavel Malinovskij Jun 29 '19 at 16:35
  • will get back to you in few minutes – Antonio Jun 29 '19 at 16:40
  • thank you, I can wait! I appreciate you help very much! – Pavel Malinovskij Jun 29 '19 at 16:42
  • try calling `view.renameListTitle.requestFocus()` after `customBuilder.show()` – Antonio Jun 29 '19 at 17:03
  • let the `inputHelper.showDialogKeyboard()` be commented as before – Antonio Jun 29 '19 at 17:04
  • unfortunately nothing happens, keyboard do not opens, as I understood, this is because EditText is inside of dialog... :( I also tried to use tutorial provided in link below (first approved answer), i put that code to fun makeDealog(oldListTitle: String) in different places of that function but nothing happened https://stackoverflow.com/questions/2403632/android-show-soft-keyboard-automatically-when-focus-is-on-an-edittext – Pavel Malinovskij Jun 29 '19 at 18:43
  • I just found solution, I uncommented inputHelper.showDialogKeyboard() and also inside of hideKeyboard(activity: Activity, view: View) used inputManager.hideSoftInputFromWindow(view.getWindowToken(),0) instead of inputManager.hideSoftInputFromWindow(view.windowToken, InputMethodManager.HIDE_NOT_ALWAYS) . Any way thank you a lot! You have helped my very much! – Pavel Malinovskij Jun 29 '19 at 19:31
0

Add this in your Activity in manifest -

android:windowSoftInputMode="stateHiddenAlways"

Anupam
  • 2,523
  • 2
  • 12
  • 28
0
 fun hideKeyboard(activity: Activity) {
        val imm = activity.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
        //Find the currently focused view, so we can grab the correct window token from it.
        var view = activity.currentFocus
        //If no view currently has focus, create a new one, just so we can grab a window token from it
        if (view == null) {
            view = View(activity)
        }
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }
HandyPawan
  • 638
  • 1
  • 7
  • 11
0
recyclerViewOfLists = RecyclerViewOfLists(cursor, this, MainActivity())

You cannot instantiate activities just by calling the constructor - for example here you're instantiating a new MainActivity. Such activities are not initialised for anything you'd need an activity for, such as accessing system services or otherwise to be used as Context.

Instead of creating a new instance, pass a reference to an existing one with this, e.g.

recyclerViewOfLists = RecyclerViewOfLists(cursor, this, this)
laalto
  • 137,703
  • 64
  • 254
  • 280