Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieve Context from a fragment

Tags:

I created a class to retrieve comments from a JSON encoding from a PHP file. This class, extends from AsyncTask:

public class RecuperarComentarisFoto extends AsyncTask<String, String, String>{

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(????);
        pDialog.setMessage("Creating Product..");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    @Override
    protected String doInBackground(String... arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    protected void onPostExecute(String result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
    }

}

As you can see, I'm trying to show a ProgressDialog while the "doInBackground" proccess is working. But progressDialog constructor, asks for a Context, and I don't know how to provide it.

I'm calling this class from a Fragment, so I can't access the context like this:

pDialog = new ProgressDialog(MyFragmentA.context);

The "main" acitivity is called: AndroidViewPagerActivity which extends FragmentActivity.

(By main, I mean that it's the one that is creating the tabs, and managing the navigation between them.)

This is it's code:

public class AndroidViewPagerActivity extends FragmentActivity {
ViewPager mViewPager;
TabsAdapter mTabsAdapter;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);
    setContentView(mViewPager);

    final ActionBar bar = getActionBar();
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    bar.setDisplayOptions(0, ActionBar.DISPLAY_SHOW_HOME);
    bar.hide();

    mTabsAdapter = new TabsAdapter(this, mViewPager);
    mTabsAdapter.addTab(bar.newTab().setText("Fragment A"), MyFragmentA.class, null);

    if (savedInstanceState != null) {
        bar.setSelectedNavigationItem(savedInstanceState.getInt("tab", 0));
    }

}

How can I access the context? Which context should I use? "AndroidViewPagerActivity" context? So how can I access its context from a fragment?

Thank you.

Sergi

like image 647
Reinherd Avatar asked Jan 04 '13 17:01

Reinherd


People also ask

How do I get context in Mainactivity fragment?

You can use the getActivity() method to get context or You can use getContext() method . View root = inflater.

What is a fragment activity?

A Fragment represents a reusable portion of your app's UI. A fragment defines and manages its own layout, has its own lifecycle, and can handle its own input events. Fragments cannot live on their own--they must be hosted by an activity or another fragment.

What is context android?

Definition. it's the context of current state of the application/object. It lets newly-created objects understand what has been going on. Typically, you call it to get information regarding another part of your program (activity and package/application).


2 Answers

Use getActivity() inside the Fragment to obtain a Context that you can pass along. That works, as Activity inherits from Context.

As alternative you can use getApplicationContext() to obtain the Context.

like image 149
Heiko Rupp Avatar answered Oct 17 '22 17:10

Heiko Rupp


@heiko-rupp's answer is outdated, Fragment.getContext() has been introduced in Fragments in API 23.

To pass it around from fragment you have to use, like this.getContext()

https://developer.android.com/reference/android/app/Fragment.html#getContext() which according to documentations just

Return the Context this fragment is currently associated with.

like image 24
ishandutta2007 Avatar answered Oct 17 '22 18:10

ishandutta2007