gpt4 book ai didi

android - 销毁android中的先前 fragment

转载 作者:行者123 更新时间:2023-11-30 01:47:40 25 4
gpt4 key购买 nike

在我的项目中,我在这些 fragment 之一的 android fragment 之间航行,有一个从 json 接收信息的列表。有必要每 1 分钟更新一次此列表,我正在使用

postDelayed (this, 60000);

调用一个线程,它使用changeActivity (ActivitiesListFragment.class, null); 刷新列表。

正在运行,列表在新 fragment 中更新。

问题是当我按下后退按钮时,它返回到之前的列表,因为它没有被“销毁”。

如何开始一个新 fragment 并删除之前的 fragment ?谢谢!!

public class ActivitiesListFragment extends AbstractListFragment {
private AbstractNetworkHandler handler;
private ListView listView;
private ActivitiesListAdapter adapter;

private long currentPage = 0L;
private int pageSize = 50;
private int previousTotal = 0;
private boolean loading = true;
private boolean isPaused;

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

Handler handler;
handler = new Handler();

super.setupListUI(NetworkAction.ACTIVITIES_LIST, rootView, true);

this.handler = AbstractNetworkFactory.build(super.getContext(), super.networkAction);
this.listView = (ListView) rootView.findViewById(R.id.list_view_activities);

this.listView.setOnScrollListener(new OnScrollListener() {
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
}

@Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if(totalItemCount < pageSize) {
return;
}

if(loading) {
if(totalItemCount > previousTotal) {
loading = false;
previousTotal = totalItemCount;
currentPage++;
}
}
if(!loading && (totalItemCount == (firstVisibleItem + visibleItemCount))) {
loading = true;
Editable text = ActivitiesListFragment.super.txtFilter.getText();
if(text != null && text.toString() != null && !text.toString().isEmpty()) {
refreshGrid(text.toString(), null);
}
else {
refreshGrid(null, null);
}
}
}
});

if(this.isPaused) {
this.listView.setAdapter(adapter);

Bundle arguments = getArguments();
if(arguments != null) {
String mode = arguments.getString("mode");
if("search".equals(mode)) {
Editable text = this.txtFilter.getText();

String toFilter = text.toString();
this.adapter.clearItems();
this.adapter.notifyDataSetInvalidated();
this.currentPage = 0;
this.previousTotal = 0;

if(toFilter != null && !toFilter.isEmpty()) {
refreshGrid(toFilter, null);
}
else {
refreshGrid(null, null);
}
}
}
return rootView;
}

if(!ApplicationOptionsManager.hasOptionsCache(super.getActivity(), networkAction)) {
final Count c = new Count();
// this.refreshOptions(true);// false para adicionar "Listar Todos" no
final HomeActivity home = (HomeActivity) getActivity();

final Runnable myRunnable= new Runnable() {
@Override
public void run()
{
refreshOptions(true);
adapter.notifyDataSetChanged();
handler.postDelayed(myRunnable, 60 * 1000);
}
};

return rootView;
}

private void refreshOptions(final boolean isToLoad) {
final ProgressDialog dialog = MobileUtils.getBasicProgressDialog(super.getContext());
handler.getOptions(new HttpOptionsBaseListener() {
@Override
public void onRequestCompleted(OptionsVO options, Integer httpStatus, CharSequence response) {
ApplicationOptionsManager.storeOptions(ActivitiesListFragment.super.getActivity(), networkAction, options);
if(isToLoad) {
String txtToFilter = txtFilter.getText().toString();

if(txtToFilter != null && !txtToFilter.isEmpty()) {
refreshGrid(txtToFilter, dialog);

}
else {
refreshGrid(null, dialog);

}
}
else {
dialog.dismiss();
// addOne(); "Listar todos" no começo do fragment
}
}
}, new HttpFailListener() {
@Override
public void onRequestCompleted(Exception e, Integer httpStatus, CharSequence msg) {
String txtToFilter = txtFilter.getText().toString();

if(txtToFilter != null && !txtToFilter.isEmpty()) {
refreshGrid(txtToFilter, null);
}
else {
refreshGrid(null, null);
}

dialog.dismiss();
}
});
}

private void refreshGrid(final CharSequence textToFilter, final ProgressDialog currentDialog) {
super.runRunnableOnUIThread(new Runnable() {
@Override
public void run() {
final ProgressDialog dialog = currentDialog != null ? currentDialog : MobileUtils.getBasicProgressDialog(getContext());

JSONObject where = new JSONObject();

if(textToFilter != null && !textToFilter.toString().isEmpty()) {
try {
where.put("nmStep", textToFilter);
}
catch(JSONException e) {
}
}

handler.get(currentPage, new HttpJsonObjectListener() {

@Override
public void onRequestCompleted(final JSONObject object, Integer httpStatus, CharSequence msg) {
final JSONArray array = object.optJSONArray("searchResultsCollection");
ActivitiesListFragment.super.runRunnableOnUIThread(new Runnable() {
@Override
public void run() {
dialog.dismiss();

if(adapter != null) {
int count = adapter.getCount();
if(count > 0) {
if(array.length() > 0) {
adapter.addAll(array);
}
else {
currentPage--;
if(currentPage < 0) {
currentPage = 0;
}
}
}
else {
String[] columns = { "nmStep" };
if(array.length() == 0) {
JSONObject value = new JSONObject();

try {
value.put("id", -1);
value.put("nmStep", "Nenhum resultado encontrado.");

}
catch(Exception e) {
}

array.put(value);
}
adapter = new ActivitiesListAdapter(array, columns, ActivitiesListFragment.this);
listView.setAdapter(adapter);
}
}
else {

if(array.length() == 0) {
JSONObject value = new JSONObject();

try {
value.put("id", -1);
value.put("nmStep", "Nenhum resultado encontrado.");

}
catch(Exception e) {
}

array.put(value);
}

String[] columns = { "nmStep", "dtStartFmt" };
adapter = new ActivitiesListAdapter(array, columns, ActivitiesListFragment.this);
listView.setAdapter(adapter);
}
}
});
}
}, new HttpFailListener() {
@Override
public void onRequestCompleted(Exception e, Integer httpStatus, CharSequence msg) {
dialog.dismiss();
ActivitiesListFragment.super.runToastOnUiThread(R.string.application_generic_error, Toast.LENGTH_LONG);
}
});

}
});
}

protected int getContentViewId() {
return(R.layout.fragment_activities_list);
}

@覆盖 protected void onFilterForText(CharSequence textToFilter) { 如果(适配器!= null){ 适配器.clearItems(); 当前页 = 0; 以前的总数= 0; } refreshGrid(textToFilter.toString(), null);

@Override
protected int getTitleResource() {
return R.string.title_activity_activities_list;
}

@Override
public void onPause() {
handler.removeCallbacks(myRunnable);
this.isPaused = true;
super.onPause();
}

@Override
public void onResume() {
handler.postDelayed(myRunnable, 60 * 1000);
super.onResume();
}

public class Count {
private int count;

public int getCount() {
return count;
}

public void setCount(int count) {
this.count = count;
}
}

public View getChildAt(int position) {
return(this.listView.getChildAt(position));
}

最佳答案

tiny sunlight's帖子回答你的问题。但是他提到当您只想更新 ListView 时不应该创建新的 Fragment。实际上,实现此目的的一种方法是在处理程序中更新列表的内容,而不是创建新的 fragment 。实现以下目标的代码段:

Handler handler;

@Override
protected void onCreate(Bundle savedInstanceState)
{
....
handler = new Handler();
}

@Override
protected void onResume()
{
....
handler.postDelayed(myRunnable, 60 * 1000);
}

@Override
protected void onPause()
{
....
handler.removeCallbacks(myRunnable);
}

private final Runnable myRunnable= new Runnable() {
@Override
public void run()
{
downloadNewJsonData();
adapter.notifyDataSetChanged();
handler.postDelayed(myRunnable, 60 * 1000);
}
}

这样做的好处是避免了替换 fragment 的内存、时间和代码开销。

关于android - 销毁android中的先前 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33480428/

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