简体   繁体   中英

How to update a Fragment after an AsyncTask finishes in Android?

There is probably a very simple way to do this that will only require one or two lines of code, but I'm not sure how to go about this.

I want to have fragments that initially display a loading gif in the center of the screen, then it will start an AsyncTask which downloads information from my server. After the AsyncTask is complete, I will need to set the content view of the Fragment to a different xml file, then display some of what I got from the server.

I was thinking there should be some type of function like Fragment.invalidate() or Fragment.refresh() that could help update the Fragment, but I can't seem to find any such function.

Also, if I update the Fragment in a different function outside of the onCreateView function, do I need to create variables to hold the LayoutInflater and ViewGroup objects that are initially passed to it, or can I access these using function calls? Below is roughly what I want to do...

public class HomeFragment extends Fragment
{
    public HomeFragment(){}

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        View view = inflater.inflate(R.layout.loading, container, false);

        (new GetDataTask()).execute(1);

        return view;
    }


    private class GetDataTask extends AsyncTask<Integer, Void, ServerResponse>
    {
        @Override
        protected void onPreExecute(){}

        @Override
        protected ServerResponse doInBackground(Integer... args)
        {
            int page_num = args[0];
            return Server.getPosts(page_num);
        }

        @Override
        protected void onPostExecute(ServerResponse result)
        {
            /*
                1. Update the content view to R.layout.multi_post_list
                2. Change specific text views and image views (I know how to do this part)
                3. Notify that changes have been made to the Fragment, so it needs redrawn
            */
        }
    }
}

Note: ServerResponse is a class that I created, and Server.getPosts is a static function that returns a ServerResponse. I know that these work in my case. I need help with points 1 and 3 in my onPostExecute part of my GetDataTask class.

Thanks!

You can use findFragmentByTag

DetailsFragment df = getFragmentManager().findFragmentByTag("details");
    if (df != null) {
        df.setShownIndex(getSelectedIndex());
    } else {
        df = DetailsFragment.newInstance(getSelectedIndex());
    }
    fragmentTransaction.replace(R.id.frame, df, "details").commit();

Three things:

  1. Use AsyncTaskLoader instead of AsyncTask to prevent reloading your data after a screen rotation. Or call setRetainInstance(true) on the Fragment to prevent the Fragment from being destroyed on rotation. (just be careful to handle detached state)
  2. You can get the Activity's root view by calling getActivity().findViewById(android.R.id.content) and manipulate it however you need to. You could also just create one layout that has both states and show/hide parts of them has needed.
  3. You can get the inflater at any time by calling LayoutInflater.from(getActivity())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM