简体   繁体   中英

OnActivityResult always return null Intent data in Android

In my project I show the contact screen, When I click any contact then it should go back to my dummy screen where I need to show selected contact.

Now problem is When I click from my project contact list and selected the contact it always comes to my Dummy project screen but data always comes as null. Not getting why its comes as null. In my project for showing the contact screen we followed the lot of path.

1 HomeActivity->TabActivity->HomeTabFragment->BuddyFragment(For showing the contact screen )

In Buddy fragment I wrote the following code:

 Intent intent = new Intent();

 intent.putExtra("chaton_buddy_number", buddy.getNo());
 intent.putExtra("chaton_buddy_name", buddy.getName());

 getActivity().setResult(Activity.RESULT_OK, intent);

 getActivity().finish();
 return true;

and in dummyApplication I wrote the following code.

public class MainActivity extends Activity { 
TextView tv; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
tv = (TextView)findViewById(R.id.textView1); 
Button button =(Button)findViewById(R.id.button1); 
button.setOnClickListener(new Button.OnClickListener() {

         @Override
         public void onClick(View v) {
              Intent intent = new Intent();
              intent.setAction(Intent.ACTION_SEND);
              intent.putExtra("mChatONBuddies", true);

    //Launching HomeActivity of my project because that one is the first activity. 
    //intent.setComponent(new ComponentName("com.sec.myproject", "com.sec.chaton.HomeActivity"));

            startActivityForResult(intent, 0);
        }
    });
}

Intent (data) always come in on my dummy project screen.

I tried following things but did not helped me.

  1. calling getParent().getAcitivity().setResult(Activity_ResultOK, intent)
  2. Removed the Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP wherever its used. Its also not working

any other suggestion is highly appreciated.

I am fan of interface when doing such callbacks

If you want your fragment should pass data back to its containing activity then consider using interface to handle and pass the data.

Step1

Declare following interface in your fragment

public interface iContactHelper {
    public void onSelectContact(String contactNumber,String contactName);
}

Step2

Declare follwoing in your Fragment.

iContactHelper icontactHelper;

@Override
public void onAttach(Activity a) {
    super.onAttach(a);
    icontactHelper = (icontactHelper) a;
}

Step3

When you need to send contact information to container activity,call following.

iContactHelper.onSelectContact(buddy.getNo(),buddy.getName());

Step4

Finally have your container activity implement the iContactHelper interface

@Override
public void onSelectContact(String contactNumber,String contactName) {
    Log.d("LOG","Contact Name " + contactName);
    Log.d("LOG","Contact Number " + contactNumber);
}

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