Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The fix of issue #310 #314

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

ArionTheWanderer
Copy link

@ArionTheWanderer ArionTheWanderer commented Jun 8, 2023

This pull request closes #310 . The bug was caused by trying to access the searchview of null value after rotating the screen, because onCreateOptionsMenu() callback is invoked only after the onResume() callback, so the searchview isn't initialized by the time the code tries to access it. Now this behaviour is preventing by additional checks.

Actually, this issue could be solved in more precise way by replacing the old way to setup the toolbar with the new API, so all the toolbar UI would be initialized by the time the code would try to access it:

protected void onCreate(Bundle savedInstanceState) {
        ...
        // Disabling the "Home" button of the old appbar
        getSupportActionBar().setDisplayHomeAsUpEnabled(false);
        getSupportActionBar().setHomeButtonEnabled(false);
        // Deleting the support action bar from the activity
        setSupportActionBar(null);

        // Toolbar setup
        mainToolbar.inflateMenu(R.menu.history_menu);
        mainToolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_add_activity:
                        Intent intentaddact = new Intent(HistoryActivity.this, EditActivity.class);
                        startActivity(intentaddact);
                        break;
                    case android.R.id.home:
                        finish();
                        break;
                }
                return true;
            }
        });
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem searchMenuItem = mainToolbar.getMenu().findItem(R.id.action_filter);
        searchView = (SearchView) searchMenuItem.getActionView();
        searchView.setIconifiedByDefault(true);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setOnCloseListener(this);
        searchView.setOnQueryTextListener(this);
        searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {
            @Override
            public boolean onSuggestionSelect(int position) {
                return false;
            }

            @Override
            public boolean onSuggestionClick(int position) {
                CursorAdapter selectedView = searchView.getSuggestionsAdapter();
                Cursor cursor = (Cursor) selectedView.getItem(position);
                int index = cursor.getColumnIndexOrThrow(SearchManager.SUGGEST_COLUMN_QUERY);
                String q = cursor.getString(index);
                searchView.setQuery(q, false);
                return false; // let super handle all the real search stuff
            }
        });
        searchView.setImeOptions(searchView.getImeOptions() | EditorInfo.IME_ACTION_SEARCH);
        ...
}

But this would require rewriting all the navigation logic and appbar code in the app, so I considered it unappropriate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Rotating the screen after entering an incorrectly formatted date in the search bar causes a crash
1 participant