当我使用ActionBar SearchView Android进行搜索时,Searchfragment多次添加到Bac



>我正在使用操作栏搜索视图。 每当发生搜索时,我都会创建一个片段,但我的问题是每当发生新搜索时,片段都会添加到 backstack 中

法典:

private void handleIntent(Intent intent) {
     if (Intent.ACTION_SEARCH.equals(getIntent().getAction())) 
      {
         QueryString = getIntent().getStringExtra(SearchManager.QUERY);
         bundle.putString("videourl",getResources().getString(R.string.serviceurl)+"/Getresults/"+((GloabalClass)getApplicationContext()).getShared().getString("StationID", "null")+"/"+QueryString);
         bundle.putBoolean("append", true);
         bundle.putString("xmlroles", session.getUserDetails().get(SessionManager.KEY_NAME).get(1));
         bundle.putString("ChannelID", "null");
         SearchRecentSuggestions suggestions = new SearchRecentSuggestions(this,
                MySuggestionProvider.AUTHORITY, MySuggestionProvider.MODE);
         suggestions.saveRecentQuery(QueryString, null);
         FragmentTransaction ft = getFragmentManager().beginTransaction();
         SearchFragment searchfragment = new SearchFragment();
         searchfragment.setArguments(bundle);
         ft.replace(R.id.content_frame, searchfragment);
         ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
         ft.addToBackStack(null);  
         ft.commit();
         mDrawerToggle.setDrawerIndicatorEnabled(false);
         getActionBar().setDisplayHomeAsUpEnabled(true);
         ((GloabalClass)getApplicationContext()).setSearch_tab_selection(0);
      }
  }

如果已经添加片段,我如何避免创建片段的反向堆栈。

试试这个

    FragmentManager fragMan = getFragmentManager();
            Fragment newFragment = null;
            String fragTag = "yourTag";
            newFragment = fragMan.findFragmentByTag(fragTag);
            if(newFragment == null)
                newFragment = new SearchFragment();
            else
                ((SearchFragment)newFragment).publicMethod(arguments);
            FragmentTransaction transaction = fragMan.beginTransaction();
            transaction.replace(R.id.main_fragment, newFragment, fragTag);
            transaction.addToBackStack(null);
            transaction.commit();

此代码不是每次都添加新片段,而是搜索使用相同标记添加的现有片段。

相关内容

  • 没有找到相关文章

最新更新