gpt4 book ai didi

android - Listview fragment 在按下后退按钮时被重新创建

转载 作者:太空宇宙 更新时间:2023-11-03 13:24:18 26 4
gpt4 key购买 nike

我在我的 ListView fragment 中使用了加载器,它在按下“后退”按钮时重新创建。你能告诉我如何处理这个 senario 吗?

这是我的 ListView fragment 代码。这里我有一个 bool 变量,我在单击列表项时将其设置为 true。但是一旦按下后退按钮,onCreateView 将被调用,因此后退按钮将为 false。

public class GTFragment  extends SherlockFragment implements LoaderCallbacks<Cursor>{

ListView mTListview = null;
GoogleTasksAdapter mGTasksAdapter = null;
private SQLiteCursorLoader mTLoader=null;
private LoaderManager mTLoaderManager;
private String mSelectedListID = null;
private boolean mIsBackbuttonisPressed = false;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.task_home_activity, container, false);


if(!mIsBackbuttonisPressed)
getLoaderManager().initLoader(0, null, this);

mTListview = (ListView) view.findViewById(R.id.id_task_list_home_activity);

mGTasksAdapter = new GoogleTasksAdapter(getActivity());

mTListview.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> listview,
View clickedview, int position, long arg3) {
// TODO Auto-generated method stub

GoogleTaskItem item = new GoogleTaskItem();

Cursor coursor = ((GoogleTasksAdapter)listview.getAdapter()).getCursor();



if(coursor.moveToPosition(position))
{


mIsBackbuttonisPressed = true;
GoogleTaskController.get_googletask_controllerObj()
.LaunchTaskPreviewActivity();
}

}

});

mTListview.setAdapter(mGTasksAdapter);
mIsBackbuttonisPressed = false;

return view;

}

我的 fragment Activity 类代码

public class TLActivity extends SherlockFragmentActivity {

LeftSliderTaskListOptions mTaskOptionsFragment = null;
GoogleTasksFragment mTFragment = null;

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



getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

setContentView(R.layout.layout_gt_list);


// FragmentTransaction tfragment = this.getSupportFragmentManager().beginTransaction();
mTFragment = new GTasksFragment();
t.replace(R.id.id_tfragment, mTFragment);
t.commit();


}

最佳答案

代替

t.replace(R.id.id_tfragment, mTFragment); 

使用

t.add(R.id.id_tfragment, mTFragment); 

对我有用

关于android - Listview fragment 在按下后退按钮时被重新创建,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22589464/

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