Building on a previous question and using fragment based approach, the question now is How to launch a fragment from a fragment while passing a custom data object
public class MainActivity extends AppCompatActivity {
public User user = new User();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_layout, user)
.commit();
}
}
User.java list of users
public class User extends Fragment {
ListView userList;
private ArrayList<UserVO> users;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.user_list, container, false); // Inflate the layout for this fragment
userList = view.findViewById(R.id.userList);
users = getUsers(); // ArrayList
userList.setAdapter(new UserAdapter());
userList.setOnItemClickListener((adapterView, view1, i, l) -> {
Log.d("UserList", "onItemClick: " + i);
// how to launch Form.java Fragment while passing users[i]
});
return view;
}
class UserAdapter extends BaseAdapter {
@Override
public int getCount() {
return users.size();
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = LayoutInflater.from(viewGroup.getContext()).inflate(android.R.layout.simple_list_item_1, viewGroup,false);
((TextView) view.findViewById(android.R.id.text1)).setText(users.get(i).givenName());
return view;
}
}
}
Form.java which is basically to display user data in a form for update purposes.
public class Form extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.form, container, false);
return view;
}
}
Q1. Within User.java I've a placeholder in the comments, asking for how to instantiate Form.java
while passing users[i]
(a custom object with fields)
Q2. Please see if there's anything wrong with the approach in MainActivity.java
, I've seen the other approach with add method and I'm using replace here, not sure which one is right here.
Attempt
I've added following code and not happy from the result, it's overlapping with the list, I want form to have it's own view and place on the stack. Back button should take the user back to the list.
Form form = new Form();
getFragmentManager().beginTransaction()
.add(R.id.frame_layout, form)
.commit();
Edit 2 For review - based on the answer.
public interface IForm { // 1
void update(UserObject userObject);
}
public class User extends Fragments implements IForm { // 2
private Form form = new Form();
void update(UserObject userObject) {
// update user list with updated object.
}
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.user_list, container, false); // Inflate the layout for this fragment
userList = view.findViewById(R.id.userList);
users = getUsers(); // ArrayList
form.delegate = this; // 3
userList.setOnItemClickListener((adapterView, view1, i, l) -> {
Log.d("UserList_archive", "onItemClick: " + i);
form.userData = users[i]; // 4
getFragmentManager().beginTransaction()
.replace(R.id.frame_layout, form)
.addToBackStack(null)
.commit();
});
return view;
}
public class Form {
public IForm delegate; // 5
public UserObject userObject; // see 4 - to display data in the form
}