Android Refreshing Fragment View after AsyncTask

11,503

First replacing a fragment is nothing like refreshing it. Yes, it reloads every view in it and data as well. But you have to relate it with your new data. So i would recomment you to create a refresh method in your fragment and send this method your refreshed data, then notify your adapter as dataSetChanged.

To achieve this, you're gonna need to reach your currently attached fragment and call its refresh method. You can use findFragmentByTag to reach it out.

Edit: To clarify little bit more

When your AsyncTask is done, you should do something like this onPostExecute method:

ResultFragment resultFrag = (ResultFragment) getSupportFragmentManager()
                                 .findFragmentByTag("FragToRefresh");
if (resultFrag != null) {
    resultFrag.refreshData(refreshedArray);
}

And in your ResultFragment you need to have refreshData method, which is something like this:

public void refreshData(ArrayList<YourObject> data) {
   yourArray = new ArrayList<YourObject>(data);
   yourAdapter.notifyDataSetChanged();
}

And your list will be refreshed whenever your task is done.

Share:
11,503
user2352691
Author by

user2352691

Updated on June 04, 2022

Comments

  • user2352691
    user2352691 almost 2 years

    I am trying to update list fragment when an AsyncTask in my activity completes but I am not sure if I am doing something wrong. Currently, I have a button that initiates the AsyncTask:

    search = (Button)findViewById(R.id.search);
        search.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        String productname = prodname.getText().toString().trim();
                        if (NetworkManager.isOnline(getApplicationContext())){
                            // Go to Other screen
                            AsyncFetcher results = new AsyncFetcher(currActivity);
                            String _url = "http://192.168.1.3:3000/search.json?
                                           utf8=%E2%9C%93&q="+productname;
                            // progressDialog = ProgressDialog.show(
                            //    ClassifiedsActivity.this, "", "Loading...");
                            results.execute(_url);
                        } else {
                            // Throw some warning saying no internet 
                            // connection was found
                        }
                    }
                });
    

    Once my execute is finished I have following to instantiate fragment within my activity:

    ResultFragment resultfrag = new ResultFragment();
    getSupportFragmentManager().beginTransaction()
                               .replace(R.id.content_frame, resultfrag).commit();
    

    However it doesnt seem to replace my content with the list fragment. Here is my layout file:

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    
        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </LinearLayout>
    
  • user2352691
    user2352691 about 11 years
    Hi yahya, thank you for response. I added the methods you described here and also had to add "m_adapter.clear(); m_adapter.notifyDataSetChanged(); m_adapter.addAll(this.search_results);" to make the refresh data work since the adapter was still holding reference to the old "ArrayList".
  • yahya
    yahya about 11 years
    Ahh yes, it of course depends on how you handle with listView and its adapter. I'm glad that you did it :)