resultCode returns RESULT_CANCELED in onActivityResult

Asked

Viewed 189 times

2

I have two activities and a Fragment.

A Fragment ProfileFragment is embedded within the Activity ProfileActivity, that is, when I call the Activity ProfileActivity, 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 MainActivity and return the name and email I typed inside it (from your Fragment).

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

Follows the code:

  • Mainactivity: My main screen where I call the ProfileActivity
  • Profileactivity: My Activity which has a Fragment within it, called by XML.
  • Profilefragment: Man Fragment with two TextView (Name and Email) which is called by ProfileActivity.

Mainactivity... (code I call the ProfileActivity.class):

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

Profilefragment... (code where food return with putExtras in the onPause):

 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 method onActivityResult)

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

The problem is that the resultCode never comes RESULT_OK, only RESULT_CANCELED and my data.getString at all times NULL. The requestCode always comes correct (1).

Someone can help me?

Thank you in advance.

2 answers

1

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

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

...So I decided to create a method inside 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 Intent to return to the previous Activity, never use inside onDestroy, onDetach or onStop, if you want to use it, then you will have to create a complex function with getParent to solve this. In my case I found it easier to change the way of use of my app, IE, if the user returns, you will lose what you typed, only save if you click the button of Actionbar "SAVE".

Thanks!

0

It’s kind of detailed when working with communication of data between fragments with fragments or with some another Activity.

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

1 - You must 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 have been filled in. Or you can use the fragment decoupling event.

3 - In its (Mainactivity) just you implement the same interface, defining in the (Implements) the name of his Fragment afterward (.) the interface name.

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

Take an example of this: Developers Android

XD:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.