ramack / ActivityDiary

Android diary for any kind of activities
GNU General Public License v3.0
73 stars 37 forks source link

The fix of issue #310 #314

Open ArionTheWanderer opened 1 year ago

ArionTheWanderer commented 1 year ago

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.