2016-04-01 12 views
0

自分のアクティビティでBackPressedに追加して、ユーザーが終了したいことを確認するダイアログを表示します。それはうまく動作します。しかし、問題は、私の活動は検索されたいくつかのデータ(同じ活動 - しかし、新しいレイアウト)の結果を表示するSearchView機能(SearchView)があり、私は元の活動に戻るのではなく、 )私は自分のコードに入れたダイアログを表示します。この場合、私はこれを望んでいません。変更後は元のアクティビティに戻ります。私は新しいアクションでも、ここでonBackPressedます。public voidを入れてみましたが、それはうまくいきませんでしたAndroidで変更した後に同じアクティビティに戻ります

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); 

       } 

:ここに私のコードを

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(); 
    } 
} 

} 

は、CODE(抜粋)を更新します

  }); 
     } 
    } 


    @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(); 
    }  
} 

答えて

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(); 
        } 
       } 

     } 

を示しながら

は、フラグを設定する必要も、あなたは真のsomewherに、このフラグを設定する必要がありますが

+0

あなたのレイアウトを変更しているあなたはこれを行う方法を私を見ることができますか?私は初心者です!ありがとう! –

+0

私はあなたの助けを借りることができるコードの一部を追加しました – Alok

+0

ありがとう、あなたの助けをありがとう、それでも動作しません。 SearchResultsを開いてから戻るボタンを押すと、設定変更後に同じアクティビティに戻るのではなく、警告ダイアログが表示されます。 –

関連する問題