每次用户点击建议时清空 Activity
Empty Activity each time user clicks on Suggestions
我在我的 Android 应用程序中实现了可搜索 Activity,我让用户可以直接点击建议...
问题是每次您点击一个建议,如果您按回键,您会进入一个空的可搜索 activity。这不能提供良好的用户体验,我想修复它。
这是我的代码:
主要Activity
@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;
}
可搜索Activity
@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"
/>
尝试在处理用户建议点击时在 startActivity(intentDetailActivity);
之后添加 finish();
。
} 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
}
我在我的 Android 应用程序中实现了可搜索 Activity,我让用户可以直接点击建议...
问题是每次您点击一个建议,如果您按回键,您会进入一个空的可搜索 activity。这不能提供良好的用户体验,我想修复它。
这是我的代码:
主要Activity
@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;
}
可搜索Activity
@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"
/>
尝试在处理用户建议点击时在 startActivity(intentDetailActivity);
之后添加 finish();
。
} 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
}