0

my problem is to pass a string between 3 activity..I explain how I did it.

the first activity called the loginActiviy where I do this.

  //OnCreate ecc before this
     @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
     data.putExtra("Name", Name);
     super.onActivityResult(requestCode, resultCode, data);
 }

 public void onResume()
 {
     super.onResume();
     finish();
 }

when the activity ends is called finish () ( maybe that's the problem I do not know ) then control passes to the first activity, where I do this

    //OnCreate ecc before
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Bundle extras = data.getExtras();
    String name = null;
    if(extras != null) {
        name = extras.getString("Name");
    }
    super.onActivityResult(requestCode, resultCode, data);
    try
    {
        Intent people = new Intent(this, MainPeopleActivity2.class);
        people.putExtra("Name", name);
        startActivity(people);
        this.finish();
    }
    catch(Exception e)
    {
        Toast.makeText(getApplicationContext(), e, Toast.LENGTH_LONG).show();
    }
}

Now start third activity , where take the intent doing this

        String name;
    if (savedInstanceState == null) {
        Bundle extras = getIntent().getExtras();
        if(extras == null) {
            name= null;
        } else {
            name= extras.getString("Name");
        }
    } else {
        name = (String) savedInstanceState.getSerializable("Name");
    }

But as you can imagine this is not the case , the string is empty.

where am I wrong ? Thanks in advance.

This is full Login Class

                public void onSuccess(LoginResult loginResult) {
                GraphRequest.newMeRequest(
                        loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                            @Override
                            public void onCompleted(JSONObject user, GraphResponse response) {
                                if (response.getError() != null) {
                                } else {
                                    id = user.optString("id");
                                    firstName = user.optString("first_name");
                                    lastName = user.optString("last_name");
                                    email = user.optString("email");
                                    Log.i(TAG,"User ID "+ id);
                                    Log.i(TAG,"Email "+ email);
                                }
                                Name = firstName + " " + lastName;
                                Toast.makeText(getApplicationContext(), "Log in with " + Name, Toast.LENGTH_LONG).show();
                            }
                        }).executeAsync();
        }
Michele Lacorte
  • 5,323
  • 7
  • 32
  • 54
  • In the first activity, why do you cal `finish()` from `onResume()`? – TDG Jul 04 '15 at 19:19
  • It's a bit unclear exactly what you're doing. Where do you call `startActivityForResult()`? – Daniel Nugent Jul 04 '15 at 19:19
  • why not Stop Automatically. @TDG – Michele Lacorte Jul 04 '15 at 19:22
  • I not ever call the method startActivityForResult() @DanielNugent – Michele Lacorte Jul 04 '15 at 19:22
  • @MicheleLacorte If you never call startActivityForResult(), your onActivityResult() methods don't do anything. That would mean that your call to `data.putExtra("Name", Name);` never gets executed, so you're never getting the data in the `name` variable in the other Activity. – Daniel Nugent Jul 04 '15 at 19:45
  • I tried but nothing, i do firstActivity->LoginActivity (with startActivityForResult()) ->ThirdActivity (with l startActivityForResult()) – Michele Lacorte Jul 04 '15 at 20:19
  • @MicheleLacorte Do you create a new intent in LoginActivity, call 'putExtra()' with the Name, then call `setResult()`, and then finish the Activity? Take a look here on how to properly implement this functionality: http://stackoverflow.com/questions/2497205/how-to-return-a-result-startactivityforresult-from-a-tabhost-activity – Daniel Nugent Jul 04 '15 at 20:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/82382/discussion-between-daniel-nugent-and-michele-lacorte). – Daniel Nugent Jul 04 '15 at 20:29

2 Answers2

2

Here is how it looks like you could fix your issue and get your code working.

The only place that you need onActivityResult() is in Activity A (first Activity).

The first step in the process is to launch your LoginActivity using startActivityForResult(), probably from onCreate():

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Intent i = new Intent(this, LoginActivity.class);
    startActivityForResult(i, 999);

}

Then, set up your onActivityResult() method in this class, which will process the result from LoginActivity, and send the Name to MainPeopleActivity2:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //check request code and result code:
    if (requestCode == 999 && resultCode == RESULT_OK) {
        //get the extras:
        Bundle extras = data.getExtras();
        String name = null;
        if (extras != null) {
            name = extras.getString("Name");
            try {
                Intent people = new Intent(this, MainPeopleActivity2.class);
                people.putExtra("Name", name);
                startActivity(people);
                //this.finish();
            } catch (Exception e) {
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
            }
        }
    }
}

Then your onSuccess() method in LoginActivity will need some added code in here. Send the Name back to the first Activity with a call to setResult():

public void onSuccess(LoginResult loginResult) {
    GraphRequest.newMeRequest(
            loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                @Override
                public void onCompleted(JSONObject user, GraphResponse response) {
                    if (response.getError() != null) {
                        //process error
                    } else {
                        id = user.optString("id");
                        firstName = user.optString("first_name");
                        lastName = user.optString("last_name");
                        email = user.optString("email");
                        Log.i(TAG,"User ID "+ id);
                        Log.i(TAG, "Email " + email);

                        Name = firstName + " " + lastName;
                        Toast.makeText(getApplicationContext(), "Log in with " + Name, Toast.LENGTH_LONG).show();

                        //adding this:
                        Intent i = new Intent();
                        i.putExtra("Name", Name);
                        LoginActivity.this.setResult(RESULT_OK, i);
                        LoginActivity.this.finish();
                    }


                }
            }).executeAsync();
}

Then, you will be able to successfully get the Name in your MainPeopleActivity2 Activity in onCreate():

public class MainPeopleActivity2 extends ActionBarActivity {

    String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_MainPeopleActivity2);

        if (savedInstanceState == null) {
            Bundle extras = getIntent().getExtras();
            if(extras == null) {
                name= null;
            } else {
                name= extras.getString("Name");
            }
        } else {
            name = (String) savedInstanceState.getSerializable("Name");
        }

    }

  //...... rest of the code in this class
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
0

The proper way to pass a result back from called Activity to calling Activity is to use Activity.setResult(int, Intent) in the called Activity: http://developer.android.com/reference/android/app/Activity.html#setResult(int, android.content.Intent)

The calling Activity must call startActivityForResult() instead of startActivity() in order to get the result back.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51