Skip to content Skip to sidebar Skip to footer

Onactivityresult Always Returns 0

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: @Overrid

Solution 1:

Try this:

  Log.d(TAG, "***** I can see this is called ******");
        setResult(RESULT_OK, new Intent());
     finish();

Solution 2:

Try this

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

And in your onActivityResult() use this.

@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stubsuper.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

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

Good luck

Solution 3:

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.

Post a Comment for "Onactivityresult Always Returns 0"