Search code examples
androidandroid-fragmentsandroid-asynctaskandroid-gridview

Context is not available in Fragment of another Activity


I have an Activity with DrawerLayout. I am inflating an Fragment in FrameLayout of this Activity xml file. Xml file contain the GridView inside the LinearLayout. I apply setOnItemClickListener on this GridView inside the fragment onActivityCreated method. But on this method I am calling AsyncTask Class of my Activity. So I am not getting the Context when I call this AsyncTask from my setOnItemClickListener. Please give me some suggestion how to do it or any alternative if possible.

My Activity

class GetExamList extends AsyncTask<String, String, String>{
    Context context;

    public GetExamList(Context mContext){
        context=mContext;
    }

    @Override
    protected void onPreExecute() {     
     super.onPreExecute();
     pDialog = new ProgressDialog(context);
     pDialog.setMessage("Fetching Test List");
     pDialog.setIndeterminate(false);
     pDialog.setCancelable(true);
     pDialog.show();        
    }

    @Override
    protected String doInBackground(String... params) {

         exam_id=1;         
         List<NameValuePair> list = new ArrayList<NameValuePair>();
         list.add(new BasicNameValuePair("exam_id", exam_id+""));

         JSONObject json = jsonParser.makeHttpRequest(url_exam_list,
                    "POST", list);

        // check for success tag
            try {
                int success = json.getInt("flag");
                Log.d("flag", success+"");                  

                if (success == 1) {
                    //fetch exam list
                    JSONArray elist=json.getJSONArray("testdata");
                    Log.d("flag=1", "in try of HomeExamList");

                    for (int i = 0; i < elist.length(); i++) {

                        JSONObject obj=elist.getJSONObject(i);

                        int testId=obj.getInt("test_id");
                        String testName=obj.getString("test_name");

                        Log.d("elist  test_id", testId+"");
                        Log.d("elist  test_name", testName);

                        Test test=new Test();
                        test.setExam_id(exam_id);
                        test.setTest_id(testId);
                        test.setTest_name(testName);

                        Dao dao=new Dao(context);
                        dao.open();

                        boolean check=dao.chechTestIdInTestList(testId);
                        if(!check)  dao.insertTestList(test);

                        dao.close();
                    }


                } else {
                    // failed to create product
                    Toast.makeText(getApplicationContext(), "unsuccessful", 2000).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }


        return null;
    }

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

     Intent intent = new Intent(context, ExamList.class);
     intent.putExtra("eid", exam_id);                    
     startActivity(intent);

    }           
}

Solution

  • There are ways to get the context:

    If you are in an Activity:

    this;//will call your activity's context
    getApplicationContext();//will get the whole application context
    

    If you are in a Fragment:

    getActivity();//will call the activity context
    

    If you are getting the context inside an inner class ex. Asynctask in a Fragment:

    Fragment_class_name.this.getActivity();//explicitly get the reference of your fragment and call the context
    

    If you are getting the context inside an inner class ex. Asynctask in a Activity:

    Activity_class_name.this;//explicitly get the reference of your activity context
    

    Edit:

    change this:

    startActivity(intent);
    

    to this:

    context.startActivity(intent);