0

How can I call a method from fragment activity when my method placed is the Activity?

public class PosMainActivity extends AppCompatActivity {
      @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_trx);
    Bundle extras = getIntent().getExtras();
    if (extras != null) {
         response_message = extras.getString("RESPONSE_MESSAGE");

    }

    //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    //setSupportActionBar(toolbar);
    // Create the adapter that will return a fragment for each of the three
    // primary sections of the activity.
    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

    TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
    mViewPager.addOnPageChangeListener(new 
   TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.addOnTabSelectedListener(new 
       TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
     }

   public  void parseJsonResponse(String jsonResponse){
// I parses and adds the value to the text view or button here
         try {
            JSONObject ResponseHeaderObject = new JSONObject(responseData);
            String UIHeader = ResponseHeaderObject.getString("HEADER");
            //Set Header Display
            headerTextView =(TextView) findViewById(R.id.headerTextView);
            headerTextView.setText(UIHeader);

        }catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

 }

     /**
     * A placeholder fragment containing a simple view.
     */
       public static class PlaceholderFragment extends Fragment {
    @Override
    public void onViewCreated(View v, Bundle savedInstanceState) {
         super.onViewCreated(v, savedInstanceState);
         parseJsonResponse(response_message);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        TextView textView = (TextView) rootView.findViewById(R.id.section_label);
        textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));

        //Switch to different layout accordingly
        switch (getArguments().getInt(ARG_SECTION_NUMBER))
        {
            case 1: {
                rootView = inflater.inflate(R.layout.fragment_main, container, false);
                break;
            }
            case 2: {
                rootView = inflater.inflate(R.layout.fragment_receipt, container, false);
                break;
            }


        }
        return rootView;
      }
   }
}

When I place the parseJsonResponse in onViewCreated() method - I get the following error:

non static field cannot be referenced from a static context android

parseJsonResponse() is in my Activity class and I cannot change that to static method.

What I can do in this case?

Pochmurnik
  • 780
  • 6
  • 18
  • 35
Jerry Abraham
  • 1,039
  • 18
  • 42

1 Answers1

0

Solution:

Call method of Activity from fragment

 ((YourActivityName)getActivity()).addUserLineInfoFragment();

Caution: This may cause leak somethimes. Hence,

Call method of fragment from Activity

1. Create Interface

 public interface OnButtonListener
    {
        void onButtonClick();
    }

2. Init in Activity

 protected OnButtonListener onActionButtonListener;

    public void setOnButtonListener(OnButtonListener actionButtonListener)
    {
        this.onActionButtonListener = actionButtonListener;
    }

3. In Activity, click event when this action need to perform

this.onActionButtonListener.onButtonClick();

4. Implement listener(OnButtonListener) in Fragment

 @Override
    public void onButtonClick(){}

5. In fragment onCreateView

((YourActivityName) getActivity()).setOnButtonListener(this);

Source: Refer This

Ümañg ßürmån
  • 9,695
  • 4
  • 24
  • 41