Search code examples
androidandroid-intentandroid-softkeyboardandroid-input-method

Return to activity after Action Input Method Settings


I am making a custom keyboard app.
There is a button that leads the user from the app to Input Method Settings.
Here is the intent:

startActivityForResult(new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS), 2000);

Now, is there a way to return user to the activity after he enables the keyboard?
EDIT
Is there a way to set a BroadcastReceiver and register when the user clicks "ok" button on warning window? And then app can call super.onResume() to resume the activity.


Solution

  • So I simply spawn an IntentService that listens for changes at the same time I launch the Settings menu. I believe this is also how SwiftKey has done theirs.

    public class MyService extends IntentService {
    
        /**
         * Creates an IntentService
         */
        public MyService() {
            super("MyService");
        }
    
        @Override
        protected void onHandleIntent(Intent intent) {
            String packageLocal = getPackageName();
            boolean isInputDeviceEnabled = false;
            while(!isInputDeviceEnabled) {
                InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
                List<InputMethodInfo> list = inputMethodManager.getEnabledInputMethodList();
    
                // check if our keyboard is enabled as input method
                for(InputMethodInfo inputMethod : list) {
                    String packageName = inputMethod.getPackageName();
                    if(packageName.equals(packageLocal)) {
                        isInputDeviceEnabled = true;
                    }
                }
            }
    
            // open activity
            Intent newIntent = new Intent(this, MainActivity.class);
            newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(newIntent);
        }
    }