gpt4 book ai didi

java - 如何在切换Fragment时停止AsyncTask?

转载 作者:行者123 更新时间:2023-12-01 21:20:03 25 4
gpt4 key购买 nike

我正在使用底部导航栏,切换 fragment 时异步任务不起作用,需要等待8到10分钟才能从异步任务获取响应。由于Asynctask问题,所有页面都没有数据可显示。请帮我解决这个问题。谢谢 fragment 切换代码:

    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {

@Override
public boolean onNavigationItemSelected( MenuItem item) {
Fragment fragment=null;
switch (item.getItemId()) {
case R.id.navigation_home:
fragment = new HomeFragment();
break;
case R.id.navigation_market:
fragment=new MarketFragment();
break;
case R.id.navigation_agent:
fragment=new AgentFragment();
break;
case R.id.navigation_finance:
fragment=new FinanceFragment();
break;
/* case R.id.navigation_more:
MoreFragment moreFragment=new MoreFragment();
moreFragment.show(getSupportFragmentManager(),moreFragment.getTag());*/
}
closeContextMenu();
return loadFragment(fragment);
}
};
private boolean loadFragment(Fragment fragment) {
if (fragment != null) {
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, fragment)
.commit();
return true;
}
return false;
}```

异步任务代码:我已经在所有 fragment 中实现了这样的 asynctask。

        JSONObject jsonObject, jsonObject2;
@Override
protected void onPreExecute() {
super.onPreExecute();
SignupActivity.this.layoutProgress.setVisibility(View.VISIBLE);
}
@Override
protected String doInBackground(Void... voids) {
HashMap<String, String> map = new HashMap<>();
map.put("countrycode",code);
map.put("phonenumber",phone);
map.put("emailid",email);
map.put("agentname",fullname);
map.put("username",username);
map.put("password",lpassword);
map.put("confirmpassword",conlpassword);
map.put("transactionpassword",spassword);
map.put("transactionconfirmpassword",conspassword);
return new HttpHandler().sendPostRequest(Config.url + "JoinNowRegister",map);
}
@Override
protected void onPostExecute(String response) {
super.onPostExecute(response);
Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_LONG).show();
try {
jsonObject = new JSONObject(response);
if(jsonObject.getString("statuscode").equals("1")){
message = jsonObject.getString("status");
jsonObject2 = jsonObject.getJSONObject("data");
id = jsonObject2.getString("agentid");
int code=Integer.parseInt(jsonObject2.getString("countrycode"));
String phonenumber= jsonObject2.getString("phone");
storeCode(code);
storePhone(phonenumber);
Intent intent;
intent = new Intent(SignupActivity.this, OtpActivity.class);
Bundle bundle = new Bundle();
bundle.putString("agentid",id);
intent.putExtras(bundle);
startActivity ( intent );
finish();
}
else{
msg=jsonObject.getString("data");
Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
} catch (NullPointerException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
SignupActivity.this.layoutProgress.setVisibility(View.GONE);
}
}

最佳答案

在您的 fragment 中使用它,它将停止 AsyncTask...

@Override
protected void onDestroy() {
super.onDestroy();
// cancel running task(s) to avoid memory leaks
if (runningTask != null) runningTask.cancel(true);
}

关于java - 如何在切换Fragment时停止AsyncTask?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58853717/

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