Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to preserve AutoCompleteTextView's DropDown state when gets back from launched Activity

Tags:

android

Currently, when I

  • Launch a new Activity by clicking on AutoCompleteTextView's drop down
  • Close the launched Activity
  • AutoCompleteTextView's drop down is hidden.

I would like to preserve AutoCompleteTextView's drop-down state which includes

  • Drop down should not be hidden when gets back from launched Activity
  • Drop down's scroll position should be preserved.

I'm not exactly sure the reason why AutoCompleteTextView's dropdown will be hidden when I back from launched Activity. Hence, I had tried 2 things

  1. Change windowSoftInputMode of launched Activity from stateAlwaysHidden to stateUnchanged.
  2. In onActivityResult, when the launched Activity is closed, perform mSearchSrcTextView.showDropDown(); explicitly.

However, I am still facing the issue. The previous scroll position of AutoCompleteTextView's dropdown is not preserved. It is reset back to top of the list.

Here's the screen-shot to better illustrate the problem I am facing.


enter image description here

(Current AutoCompleteTextView's dropdown is scrolled to the end. I click on the last item and launch a new Activity)


enter image description here

(New Activity is launched. Now, I click on the BACK soft key twice, to close the keyboard and then close the Activity)


enter image description here

(Due to the explicit call of mSearchSrcTextView.showDropDown(); in onActivityResult, the drop down is shown again. However, its previous scrolled position is not being preserved. Start of list is being shown instead of end of list)

I was wondering, is there any way to preserved the AutoCompleteTextView's DropDown state, when closing a previous launched Activity?

like image 652
Cheok Yan Cheng Avatar asked Jan 26 '26 15:01

Cheok Yan Cheng


1 Answers

For AutoCompleteTextView, it has a method called dismissDropDown(). I believe when back from newly launched activity, this function is being triggered. So we workaround this problem by extending AutoCompleteTextView & override it's dismissDropDown().

We add a boolean flag temporaryIgnoreDismissDropDown, to indicate whether to temporarily ignore dismissDropDown.

public class MyAutoCompleteTextView extends AutoCompleteTextView {
    private boolean temporaryIgnoreDismissDropDown = false;

    .....

    @Override
    public void dismissDropDown() {
        if (this.temporaryIgnoreDismissDropDown) {
            this.temporaryIgnoreDismissDropDown = false;
            return;
        }

        super.dismissDropDown();
    }

    public void setTemporaryIgnoreDismissDropDown(boolean flag) {
        this.temporaryIgnoreDismissDropDown = flag;
    }
}

Before launching new Activity, we set dismissDropDown to true. After coming back from launched activity, dismissDropDown is called. The override method checks if temporaryIgnoreDismissDropDown is true, just set it to false & do nothing. So the real dismissDropDown is skipped.

// myAutoCompleteTextView is instance of MyAutoCompleteTextView
myAutoCompleteTextView.setTemporaryIgnoreDismissDropDown(true);

// launch new Activity
startActivity(....);

Hope this help, good luck!

like image 138
Shuwn Yuan Tee Avatar answered Jan 29 '26 04:01

Shuwn Yuan Tee