简体   繁体   中英

Problem with sending data from one activity to another

SOLVED: I fixed the problem. Thanks to Ted pointing me in the right direction of result code returning 0. I then realised that when the main Activity was called back again. It was cancelling the result. So I checked my manifest file and I had Single instance enabled....


I have spent a good while stuck on this problem. Googled for answers. But I can't seem to get anywhere. I'm new to Android and new to posting on Stack overflow so sorry if I muck up the formatting.

Ok so I have an application with a main activity. On this activity I have a button to login. When user clicks login button it opens in a new Activity(Login Activity) which basically allows the user to login. All this is working and tested. But my problem is that when the user logins I want to pass on his first name, last name, and userID back to the main activity. To try and pass the data between the two activities I tried to follow http://developer.android.com/resources/faq/commontasks.html#opennewscreen (below under the heading Returning a Result from a Screen.

Ok so here is the code on how I am doing this: Main Activity code:

logIn = (Button)this.findViewById(R.id.LogInButton);
     logIn.setVisibility(View.INVISIBLE);
     logIn.setOnClickListener(new OnClickListener() 
     {
         @Override
         public void onClick(View v) 
         {
             Intent loginIntent = new Intent(Main.this, Login.class);
             startActivityForResult(loginIntent, LOGIN_CODE);
         }
     });


     @Override 
 public void onActivityResult(int requestCode, int resultCode, Intent data) {     
   super.onActivityResult(requestCode, resultCode, data); 
   switch(requestCode) { 
     case (LOGIN_CODE) : { 
       if (resultCode == Activity.RESULT_OK) {
           this.setLogin(data);
       } 
       break; 
     } 
   } 
 }

Here is the code for the Login Activity:

  /**
  * Login to user.
  */
 private void loginCheck()
 {
     firstName = firstNameET.getText().toString();
     lastName = lastNameET.getText().toString();
     patientID = patientIDET.getText().toString();
     password = passwordET.getText().toString();

     boolean loginCorrect = management.isLoginCorrect(patientID, firstName, lastName, password);
     if(loginCorrect == true)
     {
         //Pass login data to main(QOLMeasurment) activity and bring us back to that activity.
         Intent intent = new Intent(Login.this, QOLMeasurement.class);
         Bundle bundle = new Bundle();

         bundle.putString("firstName", firstName);
         bundle.putString("lastName", lastName);
         bundle.putString("patientID", patientID);
         intent.putExtras(bundle);

         setResult(Activity.RESULT_OK, intent);

         Log.d("TEST", "TEST!");
         Login.this.finish();
         Log.d("TEST", "TEST:(");

     }
     else
     {
         Toast.makeText(this, "Login Failed.", Toast.LENGTH_LONG).show();
     }

 }

So my problem is I can't seem to get the data back to the main activity. Trying to troubleshoot it here are my toughts. From my understanding it is that the method startActivityForResult() will start listening for a result and the result will be dealt with by the method onActivityResult(); So in my login activity I setResult() and then I close the Activity which passes on a resultCode to onActivityResult() am I correct in this understanding?

If so the problem in hand seems to be that when I call Login.this.finish(); It doesn't seem to finish the activity and as a result the resultCode is never updated. Any help? I really am stuck in a rut with this. Thanks for your time!!! Bobby

In your code for the Login Activity, replace:

Intent intent = new Intent(Login.this, Main.class);

with

Intent intent = new Intent();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM