gpt4 book ai didi

android - 旋转后 fragment 字段为空

转载 作者:行者123 更新时间:2023-11-29 19:53:22 24 4
gpt4 key购买 nike

我有 fragment ,我在其中调用自定义 View 类(Dragger)

public class CanvasFragment extends Fragment {

private Dragger dragger;

public CanvasFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_canvas, container, false);

dragger = (Dragger) rootView.findViewById(R.id.dragger);

int[] symbolIndex = {};

dragger.setElementsArray(symbolIndex);
return rootView;
}

public void addIconToCanvas(int id){
dragger.addIcon(id);
}
}

我还有另一个带有 RecyclerView 和适配器的 fragment ,在其中我通过 的主机 Activity 调用 CanvasFragment 的 方法 addIconToCanvas() >CanvasFragment.

public class ItemAdapter extends RecyclerView.Adapter<ItemAdapter.ViewHolder> {

private int[] data;
private Context context;

public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView itemView;
public ViewHolder(View v) {
super(v);
itemView = (ImageView) v.findViewById(R.id.im_item);
}
}

public ItemAdapter(Context context, int[] myDataset) {
data = myDataset;
this.context = context;
}

@Override
public ItemAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_view, parent, false);
ViewHolder vh = new ViewHolder(v);
return vh;
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
holder.itemView.setImageResource(data[position]);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
((MainActivity)context).addItemToCanvas(data[position]);
}
});
}

@Override
public int getItemCount() {
return data.length;
}
}

我发现 CanvasFragment 中的 dragger 旋转后得到新的引用,但是当我从适配器调用 addIconToCanvas() 时 - dragger 为空。

我真的无法理解为什么字段获得引用然后丢失它)))

有谁知道,这有什么问题吗?

提前致谢!

附:宿主 Activity

public class MainActivity extends AppCompatActivity {

private CanvasFragment canvasFragment = new CanvasFragment();
private ItemsFragment itemsFragment = new ItemsFragment();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container_canvas, canvasFragment)
.commit();
getSupportFragmentManager().beginTransaction()
.add(R.id.container_items, itemsFragment)
.commit();
}

}

public void addItemToCanvas(int id){
canvasFragment.addIconToCanvas(id);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}
}

最佳答案

用线

private CanvasFragment canvasFragment = new CanvasFragment();

每次创建 Activity 时,您都将 Fragment 的新实例分配给变量 canvasFragment。但前提是 savedInstanceState == null 您将这个 CanvasFragment 实例添加到 UI。

Fragment 方法 onCreateView() 只有在需要显示 Fragment 时才会被调用,所以 dragger 配置更改后新创建的实例保持为空。

你能做什么?

如果您想将它添加到 UI,则只创建一个 CanvasFragment 实例。在 savedInstanceState != null 的情况下,找到已经存在的 Fragment,如下所示:

canvasFragment = (CanvasFragment)getSupportFragmentManager().findFragmentById(R.id.container_canvas);

关于android - 旋转后 fragment 字段为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36912395/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com