gpt4 book ai didi

java - 在 Android 中按返回键会自动调用 LoaderManager 吗?

转载 作者:行者123 更新时间:2023-11-30 03:08:12 26 4
gpt4 key购买 nike

我是 Android 新手。这是 LoaderManager 类,我在单击 Button 时编写了代码以初始化 Loader。当我按下设备的后退按钮时,我回到了上一个 Activity ,我再次调用初始化加载程序,它会自动启动新 Activity 而无需按任何键,即使我的初始化加载程序是在单击按钮时也是如此。

为什么会这样?

LoginActivityService.java

     public class LoginActivityService extends Activity implements
LoaderCallbacks<User>
{Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login_view_service);
context = this;
userName = (EditText) findViewById(R.id.userName1);
password = (EditText) findViewById(R.id.password1);
loginBtn = (ImageButton) findViewById(R.id.login1);
logoutBtn = (Button) findViewById(R.id.logout1);
loginBtn.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
doLogin(userName.getText().toString(),password.getText().toString());
}
});
}
public void doLogin(String userNameString, String passwordString) {
Log.i("do login button if", "login button clicked" + "username:"
+ userNameString + "passwor:" + passwordString);
if (userNameString.length() == 0 || passwordString.length() == 0) {
Toast.makeText(context, "UserName Or Password Should be Filled",
Toast.LENGTH_SHORT).show();
}
else
{ try {
getLoaderManager().restartLoader(1, null,LoginActivityService.this);
}
catch (Exception e) {
Log.i("do Login Stack Trace if Response false","do Login Stack Trace Catch Block");
e.printStackTrace();
}}
@Override
public Loader<User> onCreateLoader(int id, Bundle args) {
Log.i("in loader", "login button clicked" + "username:"+ userName.getText().toString() + "passwor:"
+ password.getText().toString());
loginLoader = new LoginLoader(context, userName.getText().toString(),
password.getText().toString(), "2013-07-10 01:18:26");
Log.i("login loader", "" + loginLoader);
return loginLoader;
}
@Override
public void onLoadFinished(Loader<User> arg0, User userInstance) {
Log.i("status", "" + userInstance);
if (userInstance == null) {
Toast.makeText(getApplicationContext(),"User Id and Password is wrong", Toast.LENGTH_SHORT).show();
}
else {
Intent intent = new Intent(LoginActivityService.this,
ProposalListActivity.class);
startActivity(intent);
}
}
@Override
public void onLoaderReset(Loader<User> arg0) {
// TODO Auto-generated method stub
// getLoaderManager().restartLoader(10000,null,LoginActivityService.this);
}
}

我的问题是我正在从服务中获取数据并且“userInstance”不为空,因此它会移动到 nextActivity 但是当我按下设备的返回键时它会返回到上一个 Activity 并且它会自动移动到我们拥有的相同 Activity 按下后退键。发生这种情况是因为我的扩展 AsyncTaskLoader 的类有 onloadinBackground() 并且再次调用此方法有人能指出我为什么会这样吗:

     public class LoginLoader extends AsyncTaskLoader<User>
{
@Override
public User loadInBackground() {
// TODO Auto-generated method stub
User listUser = LoginListService.getLoginInstance().getLoginResult(userName,password,apkVersion);
Log.d(TAG, "load in background");
return listUser;
}
}

有人可以指导我需要添加什么代码来避免自动调用 loadinBackground 吗?

最佳答案

一旦收到数据就尝试销毁加载器:

public void onLoadFinished(Loader<User> arg0, User userInstance) {
Log.i("status", "" + userInstance);
if (userInstance == null) {
Toast.makeText(getApplicationContext(),"User Id and Password is wrong", Toast.LENGTH_SHORT).show();
} else {
getLoaderManager().destroyLoader(arg0.getId());
Intent intent = new Intent(LoginActivityService.this,
ProposalListActivity.class);
startActivity(intent);
}
}

关于java - 在 Android 中按返回键会自动调用 LoaderManager 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21424908/

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