1

I have LoginActivity returns result after login. However, I am getting 0 always in onActivityResult() method. I cannot catch the cause. Here is my code:

MainActivity.java:

@Override
protected void onResume() {
    super.onResume();

    Intent intent = new Intent(this, LoginActivity.class);
    startActivityForResult(intent, R.id.login);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

     if (requestCode == R.id.login || resultCode==RESULT_OK)
    synchronizeContacts(R.id.contactList);
     }
}

LoginActivity.java:

private TaskCompleteListener<CsResult> taskCompleteListener = new TaskCompleteListener<CsResult>(){
    @Override
    public void onComplete(int requestId, CsResult result) {
        if (result==null || result.return_code!=0){
            String message = !TextUtils.isEmpty(result.return_message) ? result.return_message : getString(R.string.msg_invalid_user);
            JAppUtils.showMsg(LoginActivity.this, message, MsgType.ERROR);
            return;
        }
        Log.d(TAG, "***** I can see this is called ******");
        setResult(RESULT_OK);
        finish();
    }
};

public void loginClick(View view){
    LoginTask task = new LoginTask(this);
    task.setTaskCompleteListener(taskCompleteListener);
    task.setLoginUser("user_id", "password");
    task.execute(ServerConfig.URL);
}

When I debug or see the log, taskCompleteListener.onComplete() method calls correctly and returns MainActivity to call onActivitResult() method. RequestCode is right but resultCode is always Activity.RESULT_CANCEL.

What can I check again?

RivieraKid
  • 5,923
  • 4
  • 38
  • 47
sunghun
  • 1,424
  • 4
  • 25
  • 49

4 Answers4

2

Try this:

  Log.d(TAG, "***** I can see this is called ******");
        setResult(RESULT_OK, new Intent());
     finish();
Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
1

Try this

Intent intent = new Intent(this, LoginActivity.class);
startActivityForResult(intent, 1); 

And in your onActivityResult() use this.

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == 1 && resultCode == RESULT_OK && data != null )
    {                       
      // Here you can get the username and password         
    }       
}

And in your Login Activity

 Intent intent = getIntent();
 intent.putExtra("UserName", user_id); // Place your information.
 intent.putExtra("Password", password);
 setResult(RESULT_OK, intent);  
 finish();

Good luck

Akshay
  • 2,506
  • 4
  • 34
  • 55
  • Have you tried to debug it.And have you checked that your `putExtra()` is having some value or not? – Akshay Sep 04 '12 at 06:06
  • Sure, I tried several times but it did not work. Only requestCode was what I expected but others were not. – sunghun Sep 10 '12 at 06:19
  • Actually the setResult which is the subject of the question DOES work. I was having the same problem and that resolved it. What DIDN'T work for me was the putExtra. While it did put the extra in that instance of the intent, the values were never present in the intent received by the calling activity on the onActivityResult method. – Lord of Scripts Jun 19 '13 at 22:40
0

I am wondering something. You call the next application through onActivityResult() in onResume() method so the activity's lifecycle in your example goes like this.

onCreate -> onResume -> Next Activity -> back to main activity -> ???

and now there is my question is onActivityResult() called before onResume() or after? Because onResume() is called whenever the current activity goes in the foreground. So I am wondering if the case is that your second Activity is called each time you make the authorization process.

If you just want to directly move to the login Activity from main Activity, why don't you just put the onResume() snippet in onCreate()? That way you are sure that the authorization process is called one time and pretty much there is no difference the way I see it. I am not saying this is the solution, just providing some thought that I noticed.

10s
  • 1,699
  • 1
  • 12
  • 14
  • The reason why I put login in onResume() method is I want to keep showing login activity until an user will be successful to sign in. The user can exit my app pressing home button but I want my app always running on the top like home launcher. If I put the login in onCreate() method, the login activity will be disappeared without signing in when the user hit the home button. – sunghun Sep 04 '12 at 02:06
  • Ok. I don't completely understand why you want the `onResume()` method to start the login `Activity` but I guess there is a good reason for it. Can you please debug something? Place a breakpoint on the start of `onResume()` and in `onActivityResult()`. And then tell me which breakpoint hits first. Or with logging, whatever suits you. – 10s Sep 04 '12 at 19:08
-2

Instaed of Set the RESULT_OK set the value of the request code which must not be -1,0 and 1 as it is already used by Android.

E.g:
 startActivityForResult(5,new Intent());

and 
Intent intent = getIntent();
setResult(5, intent); 

Remember value must not be -1,0 or 1 as request code
  • this is a terrible misunderstanding of the way resultcodes work. you are SUPPOSED to use the codes android supplies to you. – katzenhut Aug 30 '16 at 10:00
  • you're confusing resultCode with requestCode. You start the activity with requestCode and you set the resultCode before finishing the child activity. – newDeveloper Jan 21 '17 at 02:25