Skip to content

Commit ab2b17f

Browse files
committed
Main: Fix virtual keyboard (IME) logic
When a user returns from the app details page, display IME only if it was not closed when they left the page in first place. If the IME was closed, it was likely that the user didn't intend to focus any more on the search view. Therefore, in such a case, the focus is cleared from the search view. Signed-off-by: Muntashir Al-Islam <muntashirakon@riseup.net>
1 parent b42efbb commit ab2b17f

1 file changed

Lines changed: 19 additions & 5 deletions

File tree

app/src/main/java/io/github/muntashirakon/AppManager/main/MainActivity.java

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
import androidx.annotation.NonNull;
2424
import androidx.annotation.Nullable;
2525
import androidx.appcompat.app.ActionBar;
26+
import androidx.appcompat.widget.SearchView;
2627
import androidx.collection.ArrayMap;
2728
import androidx.core.content.ContextCompat;
29+
import androidx.core.view.ViewCompat;
30+
import androidx.core.view.WindowInsetsCompat;
2831
import androidx.lifecycle.ViewModelProvider;
2932
import androidx.recyclerview.widget.LinearLayoutManager;
3033
import androidx.recyclerview.widget.RecyclerView;
@@ -212,11 +215,6 @@ protected void onAuthenticated(Bundle savedInstanceState) {
212215
actionBar.setCustomView(searchView, layoutParams);
213216
mSearchView = searchView;
214217
mSearchView.setIconifiedByDefault(false);
215-
mSearchView.setOnFocusChangeListener((v, hasFocus) -> {
216-
if (!hasFocus) {
217-
UiUtils.hideKeyboard(v);
218-
}
219-
});
220218
// Check for market://search/?q=<query>
221219
Uri marketUri = getIntent().getData();
222220
if (marketUri != null && "market".equals(marketUri.getScheme()) && "search".equals(marketUri.getHost())) {
@@ -524,6 +522,8 @@ protected void onStart() {
524522
}
525523
}
526524

525+
private boolean mImsVisibleBeforeLeave = false;
526+
527527
@Override
528528
protected void onResume() {
529529
super.onResume();
@@ -532,12 +532,26 @@ protected void onResume() {
532532
mBatchOpsHandler.updateConstraints();
533533
mMultiSelectionView.updateCounter(false);
534534
}
535+
if (mSearchView != null) {
536+
mSearchView.post(() -> {
537+
if (mImsVisibleBeforeLeave) {
538+
mSearchView.requestFocus();
539+
UiUtils.showKeyboard(mSearchView);
540+
}
541+
});
542+
}
535543
ContextCompat.registerReceiver(this, mBatchOpsBroadCastReceiver,
536544
new IntentFilter(BatchOpsService.ACTION_BATCH_OPS_COMPLETED), ContextCompat.RECEIVER_NOT_EXPORTED);
537545
}
538546

539547
@Override
540548
protected void onPause() {
549+
WindowInsetsCompat insets = ViewCompat.getRootWindowInsets(getWindow().getDecorView());
550+
mImsVisibleBeforeLeave = insets != null && insets.isVisible(WindowInsetsCompat.Type.ime());
551+
View focusedView = getCurrentFocus();
552+
if (focusedView instanceof SearchView.SearchAutoComplete) {
553+
focusedView.clearFocus();
554+
}
541555
super.onPause();
542556
unregisterReceiver(mBatchOpsBroadCastReceiver);
543557
}

0 commit comments

Comments
 (0)