我在我的活动中添加了onBackPressed以显示一个对话框,以确认用户是否希望退出它。它工作正常。但问题是,我的活动有一个SearchView功能(SearchView),显示搜索到的一些数据的结果(在同一个活动中 - 但是一个新的布局),然后按我的设备上的按钮onBack,而不是返回原始活动(清理) )它们显示了我在代码中添加的对话框。在这种情况下,我不想要这个。只需在更改后返回原始活动。
更新代码(EXCERPT):
boolean isSearchresultshowing = false;
if(isSearchresultshowing){
Intent intent = new Intent(this, Dicionario.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
// write logic to change your layout here
}else{
new AlertDialog.Builder(this)
.setIcon(R.drawable.favicon)
.setTitle("Exit?")
.setMessage("Are you sure you want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
}
这是我的代码:
public class Dictionary extends Activity {
public static final String PREFS_NAME = "MyPrefsFile1";
private TextView mTextView;
private ListView mListView;
String logTagString="DICTIONARY";
ArrayList<WordDefinition> allWordDefinitions=new ArrayList<WordDefinition>();
DictionaryDatabase DictionaryDatabase;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTextView = (TextView) findViewById(R.id.text);
mListView = (ListView) findViewById(R.id.list);
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_VIEW.equals(intent.getAction())) {
Intent wordIntent = new Intent(this, WordActivity.class);
this.finish();
wordIntent.setData(intent.getData());
startActivity(wordIntent);
} else if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
showResults(query);
}
}
private void showResults(String query) {
Cursor cursor = managedQuery(DictionaryProvider.CONTENT_URI, null, null,
new String[] {query}, null);
if (cursor == null) {
mTextView.setText(getString(R.string.no_results, new Object[] {query}));
getActionBar().hide(); //
} else {
int count = cursor.getCount();
String countString = getResources().getQuantityString(R.plurals.search_results,
count, new Object[] {count, query});
mTextView.setText(countString);
String[] from = new String[] { DictionaryDatabase.KEY_WORD,
DictionaryDatabase.KEY_DEFINITION };
int[] to = new int[] { R.id.word,
R.id.definition };
SimpleCursorAdapter words = new SimpleCursorAdapter(this,
R.layout.result, cursor, from, to);
mListView.setAdapter(words);
mListView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent wordIntent = new Intent(getApplicationContext(), WordActivity.class);
Uri data = Uri.withAppendedPath(DictionaryProvider.CONTENT_URI,
String.valueOf(id));
wordIntent.setData(data);
wordIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
wordIntent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(wordIntent);
}
我试图通过一个新动作将public void onBackPressed放在这里,但它没有工作
});
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){
SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setIconifiedByDefault(true);
LinearLayout linearLayout1 = (LinearLayout) searchView.getChildAt(0);
LinearLayout linearLayout2 = (LinearLayout) linearLayout1.getChildAt(2);
LinearLayout linearLayout3 = (LinearLayout) linearLayout2.getChildAt(1);
AutoCompleteTextView autoComplete = (AutoCompleteTextView) linearLayout3.getChildAt(0);
//Set the input text color
autoComplete.setTextColor(Color.RED);
// set the hint text color
autoComplete.setHintTextColor(Color.RED);
//Some drawable (e.g. from xml)
autoComplete.setDropDownBackgroundResource(R.drawable.seletor3);
if (autoComplete != null) {
autoComplete.setDropDownBackgroundResource(R.drawable.seletor3);
}
int searchImgId = getResources().getIdentifier("android:id/search_button", null, null);
ImageView v = (ImageView) searchView.findViewById(searchImgId);
v.setImageResource(R.drawable.buscado);
int closeButtonId = searchView.getContext().getResources().getIdentifier("android:id/search_close_btn", null, null);
ImageView closeButton = (ImageView) searchView.findViewById(closeButtonId);
closeButton.playSoundEffect(R.raw.click4);
closeButton.setImageResource(R.drawable.x);
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
EditText searchPlate = (EditText) searchView.findViewById(searchPlateId);
searchPlate.setTextColor(getResources().getColor(R.color.branco));
searchPlate.setHint("Buscar palavra");
int searchPlateId1 = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
// Getting the 'search_plate' LinearLayout.
View searchPlate1 = searchView.findViewById(searchPlateId1);
// Setting background of 'search_plate' to earlier defined drawable.
searchPlate1.setBackgroundResource(R.drawable.texfield_searchview_holo_light);
int searchTextId = searchPlate.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
TextView searchText = (TextView) searchPlate.findViewById(searchTextId);
if (searchText!=null) {
searchText.setTextColor(Color.WHITE);
searchText.setHintTextColor(Color.WHITE);
}
}
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchRequested();
return true;
default:
return false;
}
}
}
@Override
public void onBackPressed() {
new AlertDialog.Builder(this)
.setIcon(R.drawable.favicon)
.setTitle("Exit?")
.setMessage("Are you sure you want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
答案 0 :(得分:1)
你可以通过添加一个标志isSearchresultshowing来做到这一点,而不仅仅是重置布局,否则显示警告。
显示搜索结果时需要设置标志
@Override
public void onBackPressed() {
if(isSearchresultshowing){
isSearchresultshowing=false ;
//this flag need to be set true while you search the result
Intent intent = new Intent(this, Dicionario.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
// write logic to change your layout here
}else{
new AlertDialog.Builder(this)
.setIcon(R.drawable.favicon)
.setTitle("Exit?")
.setMessage("Are you sure you want to exit?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
}
}
如果您要更改布局
,也需要将此标志设置为true