Semicolon
Semicolon

Reputation: 1935

separating fragment code and MainActivity code

I am not good at Android, I am in learning phase. Here is my problam. The following function is under MainActivity.class

public void callReportsFragment(int position) {
    ReportsFragment cFragment = new ReportsFragment();
    Bundle data = new Bundle();
    data.putInt("position", position);

    // Setting the position to the fragment
    cFragment.setArguments(data);
    //
    FragmentManager fragmentManager = getFragmentManager();

    // Creating a fragment transaction
    FragmentTransaction ft = fragmentManager.beginTransaction();

    // Adding a fragment to the fragment transaction
    ft.replace(R.id.content_frame, cFragment);

    // Committing the transaction
    ft.commit();

}

And following is my ReportsFragment class.

package com.example.reports;

import android.os.Bundle;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.app.Fragment;




public class ReportsFragment extends Fragment{


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

        // Retrieving the currently selected item number
        int position = getArguments().getInt("position");

        // List of option
        String[] options = getResources().getStringArray(R.array.sidebar1);

        // Creating view correspoding to the fragment
        View v = inflater.inflate(R.layout.fragment_layout, container, false);

        // Getting reference to the TextView of the Fragment
        TextView tv = (TextView) v.findViewById(R.id.tv_content);

        // Setting currently selected option name in the TextView
        tv.setText(options[position]);

        return v;
    }
}

App is working fine. But what I want the callReportsFragment(int position) to look like following;

public void callReportsFragment(int position) {
    ReportsFragment cFragment = new ReportsFragment();
    cFragment.fetchReportView(position);
}

And make fetchReportView method in ReportsFragment class, which according to me look like this.

 public class ReportsFragment extends Fragment {


     @
     Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
         Bundle savedInstanceState) {

         // Retrieving the currently selected item number
         int position = getArguments().getInt("position");

         // List of option
         String[] options = getResources().getStringArray(R.array.sidebar1);

         // Creating view correspoding to the fragment
         View v = inflater.inflate(R.layout.fragment_layout, container, false);

         // Getting reference to the TextView of the Fragment
         TextView tv = (TextView) v.findViewById(R.id.tv_content);

         // Setting currently selected option name in the TextView
        tv.setText(options[position]);

        return v;
     }

     public void fetchReportView(int pos) {

         Bundle data = new Bundle();
         data.putInt("position", pos);


         this.setArguments(data);

         FragmentManager fragmentManager = getFragmentManager();


         FragmentTransaction ft = fragmentManager.beginTransaction();

         // Adding a fragment to the fragment transaction
         ft.replace(R.id.content_frame, this);

         // Committing the transaction
         ft.commit();

     }

}

Please help me to achieve this. My motive is to keep code seprated.

Upvotes: 0

Views: 143

Answers (2)

Max Ch
Max Ch

Reputation: 1257

Calling getFragmentManager() or getActivity() from inside the fragment will always return null, if this fragment is not yet attached to any activity. The correct pattern used in Android for what you're trying to achieve is to have static public method in the fragment like so:

public static ReportsFragment newInstance(int position) {
   Bundle data = new Bundle();
   data.putInt("position", position);
   ReportsFragment fragment = new ReportsFragment();
   fragment.setArguments(data);
   return fragment;
}

And then from your hosting Activity call: getFragmentManager().beginTransaction().replace(R.id.content_frame, ReportsFragment.newInstance(position)).commit();

Upvotes: 1

John Smith
John Smith

Reputation: 605

You can create public static method in your ReportsFragment class that will return new instance of your fragment prepared with certain parameters. But you should work with your FragmentManager in your Activity only.

Upvotes: 0

Related Questions