Android - Close IME settings activity after user has enabled input method

android, ime, input, keyboard, settings

Solution

I had a hard time trying to solve this problem as well. Until I realized that I could just check the AOSP implementation here. Specifically, checkout the SetupWizardActivity. What it looks like to me is that Google spins up a `Handler` which will poll for the state. Specifically, try to follow `SettingsPoolingHandler#handleMessage`

Specifically:

    step1.setAction(new Runnable() {
        @Override
        public void run() {
            invokeLanguageAndInputSettings();
            handler.startPollingImeSettings();
        }
    });


    public void startPollingImeSettings() {
        sendMessageDelayed(obtainMessage(MSG_POLLING_IME_SETTINGS),
                IME_SETTINGS_POLLING_INTERVAL);
    }

    @Override
    public void handleMessage(final Message msg) {
        final SetupWizardActivity setupWizardActivity = getOwnerInstance();
        if (setupWizardActivity == null) {
            return;
        }
        switch (msg.what) {
        case MSG_POLLING_IME_SETTINGS:
            if (UncachedInputMethodManagerUtils.isThisImeEnabled(setupWizardActivity,
                    mImmInHandler)) {
                setupWizardActivity.invokeSetupWizardOfThisIme();
                return;
            }
            startPollingImeSettings();
            break;
        }
    }


    void invokeSetupWizardOfThisIme() {
       final Intent intent = new Intent();
       intent.setClass(this, SetupWizardActivity.class);
       intent.setFlags(Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
               | Intent.FLAG_ACTIVITY_SINGLE_TOP
               | Intent.FLAG_ACTIVITY_CLEAR_TOP);
       startActivity(intent);
       mNeedsToAdjustStepNumberToSystemState = true;
   }

Problem

How can I close the IME settings activity (ACTION_INPUT_METHOD_SETTINGS) after the user has enabled a specific IME (or maybe just any ime if that's the only possibility) just like the Google Keyboard does. I've tried both: ``` Intent enableIntent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS); //enableIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); activity.startActivityForResult(enableIntent, 0); ``` and: ``` Intent enableIntent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS); enableIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(enableIntent); ``` But it isn't able to return to the calling activity once an item is selected/enabled. The google keyboard opens the IME input settings screen and as soon as the keyboard is enabled (check box clicked), the settings activity closes and the user is returned to the prior wizard activity.

Original source