Reputation: 95
I have created a fragment I would like to add multiple instances of:
public class myFragment extends Fragment {
int id;
public static myFragment newInstance(int id) {
myFragment fragment = new myFragment();
int id = id;
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView idText = (TextView)
rootView.findViewById(R.id.idText);
idText.setText(id);
return rootView;
}
In my Main class,
private int currentID;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
currentId = 0;
FragmentManager manager = getFragmentManager();
FragmentTransaction fragmentTransaction =
manager.beginTransaction();
oneFragment = new myFragment();
oneFragment.newInstance(currentId);
currentId = 2;
fragmentTransaction
.replace(R.id.container, oneFragment)
.commit();
twoFragment = new myFragment();
twoFragment.newInstance(currentId);
currentId = 3;
The problem is, oneFragment
is coming up with id 2. I am not entirely sure why this is the case. The container is a LinearLayout
with a vertical orientation and the Fragment
is a linear as well. I would like to use the code I wrote for myFragment
repeatedly for all of the ID's.
Upvotes: 2
Views: 1390
Reputation: 8478
You should pass id
for each Fragment using Constructor. So, every fragment instance hold their id
in member variable
public class MyFragment extends Fragment {
private int id;
// constructor
public MyFragment() {
}
public MyFragment(int a) {
this.id = a;
}
// this method is not requreied if you use above Constuctor
public myFragment newInstance(int id) {
return new myFragment(id);
}
}
Main Class :
private int currentID = 0;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
currentId = 0;
FragmentManager manager = getFragmentManager();
FragmentTransaction fragmentTransaction =
manager.beginTransaction();
oneFragment = new myFragment(currentID); // id = 0
currentId = 2;
fragmentTransaction
.replace(R.id.container, oneFragment)
.commit();
twoFragment = new myFragment(currentID); // id = 2
currentId = 3;
}
Upvotes: 0
Reputation: 954
Your MyFragment class needs to transfer the id like this:
public class MyFragment extends Fragment {
private final static String ID_KEY = "id_key";
public int id;
public static MyFragment newInstance(int id) {
MyFragment fragment = new MyFragment();
Bundle args = new Bundle();
args.putInt(ID_KEY, id);
fragment.setArguments(args);
return fragment;
}
public MyFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
id = getArguments().getInt(ID_KEY);
}
}
}
I don't think that you need to create a fragment and then call newInstance() on it. I think you can just get away with calling newInstance(). Try this:
private int currentID;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
currentId = 0;
FragmentManager manager = getFragmentManager();
FragmentTransaction fragmentTransaction =
manager.beginTransaction();
MyFragment oneFragment = MyFragment.newInstance(currentId);
currentId = 2;
fragmentTransaction
.replace(R.id.container, oneFragment)
.commit();
MyFragment twoFragment = MyFragment.newInstance(currentId);
currentId = 3;
Upvotes: 1