4 回答
TA贡献1799条经验 获得超8个赞
在代码中,替换时将创建新片段。
TaskAssignFragment taskAssignFragment = new TaskAssignFragment();
taskAssignFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
new TaskAssignFragment()).addToBackStack(null).commit();
使用已在其中设置参数的已创建的片段实例
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
taskAssignFragment).addToBackStack(null).commit();
TA贡献1842条经验 获得超21个赞
您正在创建新对象,同时替换片段。
TaskAssignFragment taskAssignFragment = new TaskAssignFragment();
taskAssignFragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
taskAssignFragment).addToBackStack(null).commit();
试试这个
TA贡献1887条经验 获得超5个赞
tagsView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Bundle bundle = new Bundle();
TextView textView = (TextView) view.findViewById(R.id.tagName);
String selectedItemName = textView.getText().toString();
bundle.putString("tagName", selectedItemName);
TaskAssignFragment taskAssignFragment = new TaskAssignFragment();
taskAssignFragment.setArguments(bundle);
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.commit();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
taskAssignFragment).addToBackStack(null).commit();
Log.i("ListView", selectedItemName);
}
});
请尝试此代码,创建一个新的 TaskAssignFragment() 实例,并在没有参数的情况下传递它
TA贡献1880条经验 获得超4个赞
您可以直接在片段上调用方法,例如:
TaskAssignFragment taskAssignFragment = new TaskAssignFragment();
// Call here your own method (from fragment)
taskAssignFragment.setSelectedItemName(selectedItemName);
在你的片段中:
public void setSelectedItemName(String selectedItemName) {
// Store data as field in class
this.selectedName = selectedItemName
}
其中 是类中的字段。selectedName
添加回答
举报