gpt4 book ai didi

android - fragment 是在其他 fragment 的后按上重新创建的

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:43:12 26 4
gpt4 key购买 nike

我在 fragment 方面遇到了问题。在我的场景中,

有两个 fragment 与 FragmentActivity 关联。在 FragmentActivity 中,有一个容器布局(Frame Layout),所有的 fragment 都将在其中替换。

public void replaceFragment(Fragment fragmentClass) {
String selectedFragment = fragmentClass.getClass().getName();


FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction
.replace(R.id.content_frame, fragmentClass);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

}

第一次,我设置了一个列表类型 fragment ( fragment A),其中从 Web 服务获取数据并填充到 ListView 中。我从 onCreateView() 方法执行 AsyncTask。

在 MainActivity 中:onCreate

SherlockFragment fragment = new FragmentA();
replaceFragment(fragment);

在Fragment A点击列表项时,Fragment A会回调Activity方法,将其替换为详情类型的Fragment Fragment B。

@Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
callback = (ICallBack) activity;

}


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

ListView list = (ListView) rootView
.findViewById(R.id.listView);
adapter = new MyListAdapter();
list.setAdapter(adapter);
list
.setOnItemClickListener(new AdapterView.OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent,
View convertView, int position, long id) {
SherlockFragment fragment = new SalonDetailFragment();
callback.replaceFragment(fragment);
}
});

ListDataTask task = new ListDataTask();
task.execute();

return rootView;
}

class ListDataTask extends AsynTask<Void,Void,List<Data>>{

public Void doInBackground(Void parems){

List<Data> = getDataFromServer();
}
onPostExecute(List<Data> data){
adapter.addAllData(data);
adapter.notifyDataSetChanged();
}

}

当我按下后退按钮时,应用程序会从 fragment B 显示 fragment A,但它会再次执行 Asynctask 并获取数据以填充 ListView 。

所以我需要知道,如何像Activity一样维护Fragment的渗透状态。

有没有办法在从其他 Activity 回来后不创建 fragment

看看我的伪代码。

最佳答案

我找到了解决方案。简单....检查rootview的空值

public class FragmentA extends Fragment {
View _rootView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (_rootView == null) {
// Inflate the layout for this fragment
_rootView = inflater.inflate(R.layout.fragment_a, container, false);
// Find and setup subviews
_listView = (ListView)_rootView.findViewById(R.id.listView);
...
} else {
// Do not inflate the layout again.
// The returned View of onCreateView will be added into the fragment.
// However it is not allowed to be added twice even if the parent is same.
// So we must remove _rootView from the existing parent view group
// (it will be added back).
((ViewGroup)_rootView.getParent()).removeView(_rootView);
}
return _rootView

关于android - fragment 是在其他 fragment 的后按上重新创建的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24990868/

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