Question

I am newbie in Android. Here is the LoaderManager class I have written code on Button click to initialize Loader. When I am pressing back button of device I came to previous activity and my call to Initialize loader is again called and it automatically starts new activity without pressing any key even my initialize loader is on button click.

Why is it happening so?

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);
    }
}

My problem is I am getting data from Service and "userInstance" is not null so it moves towards nextActivity but when I press back key of device it comes to the previous activity and it automatically moves to the same activity from where we have pressed back key. This is happening because my class that extends AsyncTaskLoader have onloadinBackground() and this method is being called again can someone point me why this is happening:

     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;
    }
     }   

Can someone please guide me what code I need to add in this to avoid automatic call of loadinBackground?

Was it helpful?

Solution

Try to destroy the loader once you received data:

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);
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top