0

in fragment when i click an image view a activity opened for get number and return result to fragment and show in edit text that it is in fragment.

myfragment:

public class Fragmenttellsms extends Fragment implements View.OnClickListener {

    ViewGroup v;
    ImageView iv;
    EditText etnumber;
    Button btnTellAFriend;
    public static String num;
    public static String NUM_KEY = "CUSTOM_TEXT";
    Intent intent;
    String[]  parts;
    private String num2;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        v = (ViewGroup) inflater.inflate(R.layout.tabfragmenttellsms,
                container, false);
    //  set();
        iv = (ImageView) v.findViewById(R.id.ivTellAFriend);
        etnumber = (EditText) v.findViewById(R.id.etTellAFriend);
        btnTellAFriend = (Button) v.findViewById(R.id.btnTellAFriend);
        num ="";    

            iv.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    if (etnumber.length()>0) {
                        num = etnumber.getText().toString()+",";
                    }
                    else {
                        num = etnumber.getText().toString();        
                    }
                    intent = new Intent(getActivity(), ContactsBackup.class);
                    intent.putExtra("oldValue", num);
                    startActivityForResult(intent,1010);;
                }
            }); 
        //    etnumber.setText("2563");
        return v;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        // Don't forget to check requestCode before continuing your job
        if (requestCode == 1010 | resultCode == 1010) {
            // Do your job
        num2 = data.getExtras().getString("valueId");
            etnumber.setText(num2);
        }
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

    }

}

Myactivity:

public class ContactsBackup extends Activity {

    Context context = ContactsBackup.this;
    ListView list;
    ArrayList<String> contactsList,contactsList2;
    protected void onCreate(Bundle savedInstanceState) {

        try {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.contactsbackup);
            overridePendingTransition(R.anim.trans_left_in,
                    R.anim.trans_left_out);

            set();

            lca = new LoadContactsAyscn();
            lca.execute();

            list.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    String s;
                    oldvalue = getIntent().getExtras().getString("oldValue");
                    s = oldvalue + contactsList2.get(position).toString();
                    s = s.replace("-", "");
                    s = s.replace(" ", "");
                //  Toast.makeText(context, s, Toast.LENGTH_LONG).show();           

                    Intent intent = new Intent();
                    intent.putExtra("valueId", s);
                    setResult(1010, intent); 
                    finish();
                }
            });
}

but edit text always empty. i see this questions but dose note work for me.

Send data from activity to fragment in android

How to pass data from activity to fragment

Community
  • 1
  • 1

2 Answers2

0

If your fragment is in a parent fragment, the Activity which hold the fragment will call the parent fragment's onActivityResult which is direct held by the activity.

You should add your fragment and activity which hold fragment code.

here is a simple example about Activity and Fragment:

//Activity
public void onCreate(Bundle saved){
    // ... init code and view 

    mFragment = new Fragmenttellsms();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.add(frame.getId(), mFragment).commit();
}

// onActivityResult, after the new activity which started from startActivityForResult finish, this method will be called.
public void onActivityResult(int requestCode, int resultCode, Intent data){
    //here you can process the result returned from the finished activity
}

the fragment I also named with Fragmenttellsms which you call startActivityForResult.

//Fragmenttellsms
...

public void onActivityResult(int requestCode, int resultCode, Intent data){
    //here you can process the result returned from the finished activity,
    //but this method will only be callback when the fragment is the child of Activity. 
    //if this Fragment is a child of another fragment which is direct child of Activity, 
    //this method will not callback.
}

for example, your Fragmenttellsms was added like this code, the onActivityResult will not callback:

// ParentFragment
getChildFragmentManager().beginTransaction().add(new Fragmenttellsms()).commit();
// in this condition, you call Fragmenttellsms object's startActivityForResult, the Fragmenttellsms's onActivityResult will not callback.

I don't know what the detail of your Activity and fragment. hope this explain can help you.

xxxzhi
  • 471
  • 3
  • 12
0

The onActivityResult method will be triggered on the parent activity of your fragment, that is the hierarchy:

//this is in your parent activity of your fragment
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)  {
    if (requestCode == YOUR_REQUEST_CODE) {
        yourFragment.onActivityResult(...);
    }
}
Iliiaz Akhmedov
  • 867
  • 7
  • 17
  • on image view click i put this code: intent = new Intent(getActivity(), ContactsBackup.class); intent.putExtra("oldValue", num); startActivityForResult(intent,1010); my activity i put this: Intent intent = new Intent(); intent.putExtra("valueId", s); setResult(1010, intent); finish(); and on activity result: if (requestCode == 1010) { num2 = data.getExtras().getString("valueId"); etnumber.setText(num2); } } who I should use your code – Ahmad Hashemi Oct 08 '15 at 08:36