将结果表单活动转换为片段

时间:2015-10-07 13:36:46

标签: android android-fragments android-activity

在片段中,当我单击图像视图时,为获取数字打开一个活动并将结果返回到片段,并在编辑文本中显示它是片段。

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();
                }
            });
}

但编辑文字始终为空。 我看到了这个问题,但是剂量注释对我来说很有用。

Send data from activity to fragment in android

How to pass data from activity to fragment

2 个答案:

答案 0 :(得分:0)

如果您的片段位于父片段中,则保存片段的Activity将调用父片段的onActivityResult,该片段由活动直接保存。

您应该添加包含片段代码的片段和活动。

这是一个关于Activity和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
}

片段我也用Fragmenttellsms命名,你称之为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.
}

例如,像这段代码一样添加了Fragmenttellsms,onActivityResult不会回调:

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

我不知道您的活动和片段的详细信息。希望这个解释可以帮助你。

答案 1 :(得分:0)

onActivityResult方法将在片段的父活动上触发,即层次结构:

//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(...);
    }
}