0

I have a class (shown below) which extends Fragment. Now i need my class to return a view to the Other class when i create an object. Everything is working fine but i need this class to do the stuff in a background thread. I also used ASYNC task but i couldn't make an ASYNC task to return a view. can anyone help.??

MapFragment.java

public class MapsFragment extends Fragment 
{  
    MapView map;
    LayoutInflater inflater_;
    ViewGroup container_;
    View layout;

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    {  
        inflater_=inflater;
        container_=container;
        super.onCreate(savedInstanceState);
        layout = inflater.inflate(R.layout.fragment_maps, container, false);
        map = (MapView) layout.findViewById(R.id.mapView);
        return (LinearLayout) layout;  
    }
}
suresh cheemalamudi
  • 6,190
  • 11
  • 49
  • 67
  • Your question is too vague. Where do you use the AsyncTask? What View do you want the AsyncTask to return? Post your AsyncTask and what you have tried to do. – Sam Dec 07 '12 at 05:42
  • I want to use Async task in above class(MapsFragment) to reurn a view from public View onCreateView() method. – suresh cheemalamudi Dec 07 '12 at 05:47

1 Answers1

2

I don't specifically know why you want to pass a View to an AsyncTask, but this is a basic approach:

public class MyAsync extends AsyncTask<Void, Void, Void> {
    private View view;

    public MyAsync(View view) {
        this.view = view;
    }

    @Override
    protected Void doInBackground(Void... params) {
        // Do something, but not with view
        return null;
    }

    @Override
    protected void onPostExecute(Void v) {
        // You can alter view here or in the other methods with UI access
        view.setVisibility(View.GONE);
    }
}

You can also use your own callbacks should this approach not apply to what you want to do.

Sam
  • 86,580
  • 20
  • 181
  • 179
  • i am using this class to return a mapview in my app. Basically i use tabs and when one of the tab is clicked, this class is instantiated to receive the mapview from the tablistener. See my question:http://stackoverflow.com/questions/13741294/how-to-build-an-app-with-actionbar-tab-support-and-mapview – suresh cheemalamudi Dec 07 '12 at 06:09