resultCode returns RESULT_CANCELED on onActivityResult

2

I have two activities and a fragment .

Fragment ProfileFragment is embedded within Activity % with%, that is, when I call ProfileActivity , it automatically opens this fragment in the center of your screen with two fields: Name and Email.

I need to open this activity ProfileActivity through my activity ProfileActivity and return the name and e-mail I typed inside it ).

The big problem is that the return always seeing with MainActivity , never resultCode == RESULT_CANCELED .

Follow the code:

  • MainActivity : My main screen where I call RESULT_OK
  • ProfileActivity : My activity which has a fragment inside it, called by XML.
  • ProfileFragment : My fragment with two ProfileActivity (Name and Email) that is called by TextView .

MainActivity ... (code that I call ProfileActivity ):

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

ProfileFragment ... (code where I feed the return with ProfileActivity.class in putExtras ):

 Intent intent = new Intent();
 intent.putExtra("EXTRA_NOME",  "Novo nome");
 intent.putExtra("EXTRA_EMAIL", "Novo email);
 getActivity().setResult(Activity.RESULT_OK, intent);

MainActivity ... (back in my main activity, in the onPause method)

if (resultCode == RESULT_OK && requestCode == 1) {
    mNome  = data.getStringExtra("EXTRA_NOME");
    mEmail = data.getStringExtra("EXTRA_EMAIL);
}

The problem is that resultCode never comes with onActivityResult , only RESULT_OK and my RESULT_CANCELED always data.getString . The requestCode is always correct ( 1 ).

Can anyone help me?

Thank you in advance.

    
asked by anonymous 24.02.2015 / 00:24

2 answers

1

Thanks for the Eudes collaboration ... Solved, come on ...

The RESULT_OK always comes as false if I put it in the onStop, onDetach, or onDestroy method. I found an article in English teaching to solve this with getParent, summarizing, very laborious ...

... So I decided to create a method within my "ProfileFragment" with the same information:

Intent intent = new Intent();
intent.putExtra("EXTRA_NOME",  "Novo nome");
intent.putExtra("EXTRA_EMAIL", "Novo email);
getActivity().setResult(Activity.RESULT_OK, intent);

In short, if you want to save some EXTRA in the attempt to return to the previous Activity, never use within onDestroy, onDetach or onStop, if you want to use, then you have to create a complex middle function with getParent to solve this. In my case I find it easier to change the way I use my app, that is, if the user returns, he will lose what he typed, he will only save if he clicks on the ActionBar button "SAVE".

Thanks!

    
25.02.2015 / 21:33
0

It's kind of detailed when working with communication data between fragments with fragments or with some other activity.

I believe that a different approach should be used to have the desired result.

1 - You should create an interface in your ProfileFragment

2 - This interface has to be implemented in the return event of the Component that has the fields that were filled. Or you can use the fragment decoupling event.

3 - In your ( MainActivity ) you simply implement the same interface, ( implements ) the name of your fragment and then (.)

4 - You implement the interface in order to get the results filled.

See an example of this: Android Developers

XD:

    
24.02.2015 / 01:05