为什么我的AsyncTask的第二次执行显示结果"附加到"第一次执行的那些?

时间:2015-08-26 19:25:51

标签: android android-asynctask callback

所以我的Activity中有一个SearchView,当用户按下搜索按钮执行搜索时,AsyncTask会被new MyTask().execute();之类的东西执行。

在这个MyTask中,我们使用基于SOAP的Web服务,SOAP调用的结果以ArrayList的形式从MyTask的doInBackground返回。这个ArrayList作为MyTask的onPostExecute中的参数被接收,我从那里调用了调用AsyncTask的Activity:

@Override
protected void onPostExecute(List<MyType> myList) {
   //****HERE I HAVE A CHECK WHICH PRINTS VALUES IN myList
  callingActivity.processResults(myList);//callingActivity is an object of the calling activity
}

processResults在执行此任务的Activity中定义,以片段形式显示结果。

问题

问题是当用户此时输入新的搜索查询时,检索到的新结果会附加到先前收到的结果中。

我在onPostExecute办理了一次检查,该帐户退出(按Log.i)列表中的值,并确认问题所在。那为什么会这样呢?有什么指针吗?

修改

在MainActivity.java中:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        ...

        searchView = (SearchView) findViewById(R.id.mainActivity_searchView);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

        handleIntent(getIntent());
    }

@Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent); // *************???? why do we do this ???
        if (searchResultsRecyclerViewFragment!=null && searchResultsRecyclerViewFragment.isVisible()) {getSupportFragmentManager().beginTransaction().remove(searchResultsRecyclerViewFragment).commit();
        } 
        handleIntent(intent);
    }

private void handleIntent(Intent intent) {
        if (intent.getAction().equals(Intent.ACTION_SEARCH)) {
            String searchQuery = SearchManager.QUERY;
            performSearch(intent.getStringExtra(searchQuery));
        }
    }

private void performSearch(String searchQuery) {
        List<MyType> searchResultsList = new ArrayList<>();
new SearchTask(this, jBProgressIndicator).execute(params);
}

public void processResults(List<MyType> searchResultsList) {

        Bundle searchResultsBundle = new Bundle();
        searchResultsBundle.putSerializable("tests.test.SEARCH_RESULTS_BUNDLE_KEY", ((Serializable) searchResultsList));

        searchResultsRecyclerViewFragment = new SearchResultsRecyclerViewFragment();
        searchResultsRecyclerViewFragment.setArguments(searchResultsBundle);

        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        fragmentTransaction.add(R.id.mainActivity_frameLayout, searchResultsRecyclerViewFragment);

        fragmentTransaction.commitAllowingStateLoss();//if I used commit(), EXCEPTION Can not perform this action after onSaveInstanceState

    }

编辑2

@Override
    protected void onPostExecute(List<MyType> list) {
        jbProgressIndicator.setVisibility(View.GONE);

        /**
         * check
         */
        Log.i(TAG, "________________________\n");
        for (MyType entry : list) {
                Log.i(TAG, "Name:> " + entry.getName() + "\n");
        }
        Log.i(TAG, "________________________\n");


        callingActivity.processResults(list);
    }

0 个答案:

没有答案