每次用户点击“建议”时清空活动

时间:2016-05-27 16:47:30

标签: android searchview

我在我的Android应用程序中实现了可搜索活动,并且我向用户提供了直接点击建议的可能性......

问题在于,每次单击一个建议时,如果按回来,则会进入空的可搜索活动。这不能提供良好的用户体验,我想解决它。

这是我的代码:

MainActivity

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search_item).getActionView();

        //Specify the searchable activity            
        searchView.setSearchableInfo(searchManager.getSearchableInfo
                (new ComponentName(this, SearchableActivity.class)));

        searchView.setIconifiedByDefault(false);  // Do not iconify the widget; expand it by default

        return true;
    }

可搜索的活动

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_searchable);
        handleIntent(getIntent(), false);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        Log.v("Activity", "onCreateOptionMenu");
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.option_menu, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search_item).getActionView();

        //Specify the searchable activity (this one)
       searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

        searchView.setSubmitButtonEnabled(true); //Submit research button
        searchView.setQueryRefinementEnabled(true); //Move the suggestion in the search bar


        searchView.setIconifiedByDefault(true);  // Do not iconify the widget; expand it by default

        return true;
    }

    @Override
    protected void onNewIntent(Intent intent) {
        Log.v("(onNewIntent", "im in NewIntent -Activity");
        // Because this activity has set launchMode="singleTop", the system calls this method
        // to deliver the intent if this activity is currently the foreground activity when
        // invoked again (when the user executes a search from this activity, we don't create
        // a new instance of this activity, so the system delivers the search intent here)
        handleIntent(intent, true);
    }


    private void handleIntent(Intent intent, boolean onNewIntent) {
        //Get the intent, verify the action and get the query
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            String query = intent.getStringExtra(SearchManager.QUERY);
            //DO MY STUFF WITH THE QUERY
            Bundle arguments = new Bundle();
            arguments.putString(QUERY_FOR_BUNDLE, query);

            SearchableFragment searchableFragment = new SearchableFragment();
            searchableFragment.setArguments(arguments);

            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.activity_searchable_container, searchableFragment)
                    .commit();

            Toast.makeText(getApplicationContext(), "(onCreate)Searching by: " + query, Toast.LENGTH_SHORT).show();

        } else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            Uri uri = intent.getData();
            int id = Integer.parseInt(uri.getLastPathSegment());

            Intent intentDetailActivity = new Intent (getApplicationContext(), DetailActivity.class);
            intentDetailActivity.putExtra("ID", id);

            Toast.makeText(getApplicationContext(), "(onCreate) Suggestion ID: " + id, Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), "(onCreate) Suggestion URI: " + uri, Toast.LENGTH_LONG).show();

            startActivity(intentDetailActivity);
          }
    }

searchable.xml

<searchable xmlns:android="http://schemas.android.com/apk/res/android"

    android:hint="HINT"
    android:label="@string/app_name"

    android:includeInGlobalSearch="true"
    android:searchSuggestAuthority="com.test.tcook.app"
    android:searchSuggestIntentAction="android.intent.action.VIEW"
    android:searchSuggestIntentData="intentTest"
    android:searchSuggestThreshold="1"
    android:searchSuggestSelection=" ?"
    android:voiceSearchMode="showVoiceSearchButton|launchRecognizer"
    />

1 个答案:

答案 0 :(得分:0)

尝试在处理用户建议时finish();点击后立即添加startActivity(intentDetailActivity);

          } else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            Uri uri = intent.getData();
            int id = Integer.parseInt(uri.getLastPathSegment());

            Intent intentDetailActivity = new Intent (getApplicationContext(), DetailActivity.class);
            intentDetailActivity.putExtra("ID", id);

            Toast.makeText(getApplicationContext(), "(onCreate) Suggestion ID: " + id, Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), "(onCreate) Suggestion URI: " + uri, Toast.LENGTH_LONG).show();

            startActivity(intentDetailActivity);
            finish(); // New line
          }