Fragment 跳转activity 并返回结果

Fragment 跳转activity 并返回结果


理论部分

Activity跳转,返回数据/结果

   需要返回数据或结果的,则使用 startActivityForResult (Intent intent, int requestCode)
,requestCode的值是自定义的,用于识别跳转的目标Activity。
   跳转的目标Activity所要做的就是返回数据/结果, setResult(int resultCode) 只返回结果不带数据,或者 s etResult(int resultCode, Intent data) 两者都返回!
   而接收返回的数据/结果的处理函数是 onActivityResult(int requestCode, int resultCode, Intent data) ,这里的requestCode就是startActivityForResult的requestCode,resultCode就是setResult里面的resultCode,返回的数据在data里面。


实现部分


1. Fragment 类实现 主要实现是红色部分

使用startActivityForResult(Intent,Activity.RESULT_FIRST_USER);跳转到activity。

重写onActivityResult(int requestCode, int resultCode, Intent data)处理返回的结果,数据在data里。 

package com.example.fragment;



import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;


import com.example.optimaltangbao.NursePrescriptionEditActivity;
import com.example.optimaltangbao.R;


public class nurse_presceiption_fragment extends  Fragment {

protected static final int SEND_SMS_REQUEST = 0;


private static final int RESULT_CANCELED = 0;


ImageButton nursePrescription;

TextView textViewdatefrom;
TextView textViewdateto;
TextView textViewdrug;
TextView textViewnote;

@Override  
    public View onCreateView(LayoutInflater inflater, ViewGroup container,  
            Bundle savedInstanceState) {  
        return inflater.inflate(R.layout.nurse_prescription_fragment, container, false);  
    }  
  
    @Override  
    public void onActivityCreated(Bundle savedInstanceState) {  
        super.onActivityCreated(savedInstanceState);  
 
        init_view_id();
        


    } 
    
    void init_view_id()
    {
        nursePrescription= (ImageButton) getView().findViewById(R.id.imageButton4);
        textViewdatefrom=(TextView) getView().findViewById(R.id.textView2);
        textViewdateto=(TextView) getView().findViewById(R.id.textView4);
        textViewdrug=(TextView) getView().findViewById(R.id.textView5);
        textViewnote=(TextView) getView().findViewById(R.id.textView6);
        
        nursePrescription.setOnClickListener(new ImageButton.OnClickListener(){
    @Override
    public void onClick(View v)
    {
    Intent Intent = new Intent(getActivity(), NursePrescriptionEditActivity.class); 
   
    Intent.putExtra("datefrom", textViewdatefrom.getText());
    Intent.putExtra("dateto", textViewdateto.getText());
    Intent.putExtra("drug", textViewdrug.getText());
    Intent.putExtra("note", textViewnote.getText());
    startActivityForResult(Intent,Activity.RESULT_FIRST_USER); 

    }
   
    });
    }
    
    @Override
public voidonActivityResult(int requestCode, int resultCode, Intent data)
       if (requestCode == Activity.RESULT_FIRST_USER) { 
   
            if (resultCode == RESULT_CANCELED) { 

                Bundle bundle = data.getExtras(); 
                textViewdatefrom.setText(bundle.getString("datefrom"));
                textViewdateto.setText(bundle.getString("dateto"));  
                textViewdrug.setText(bundle.getString("drug"));  
                textViewnote.setText(bundle.getString("note"));  
                
            } 
        }
}

}


2.activity 类实现 主要实现是红色部分

使用getIntent()接收fragment 发送的结果。

使用setResult(RESULT_CANCELED, this.getIntent().putExtras(bundle)); activity返回时返回结果该fragment。 

package com.example.optimaltangbao;


import java.util.Calendar;


import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class NursePrescriptionEditActivity extends Activity { 
    private final static int date_dialog_from = 0;
    private final static int date_dialog_to= 1;
    
    private Calendar calendar = null;


    
EditText editTextdrug;
EditText editTextnote;

TextView textViewform;
TextView textViewto;

RelativeLayout RelativeLayoutfrom;
RelativeLayout RelativeLayoutto;

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.nurse_prescription_edit);
     
        init_view_id();
    }

void init_view_id()
{
editTextdrug=(EditText)findViewById(R.id.editText1);
editTextnote=(EditText)findViewById(R.id.editText2);

textViewform=(TextView)findViewById(R.id.textView3);
textViewto=(TextView)findViewById(R.id.textView5);

RelativeLayoutfrom=(RelativeLayout)findViewById(R.id.relativeLayout11);
RelativeLayoutto=(RelativeLayout)findViewById(R.id.relativeLayout12);

Intent intent = getIntent();
editTextdrug.setText(intent.getStringExtra("drug"));
editTextnote.setText(intent.getStringExtra("note"));
textViewform.setText(intent.getStringExtra("datefrom"));
textViewto.setText(intent.getStringExtra("dateto"));


RelativeLayoutfrom.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v)
    {
    showDialog(date_dialog_from);
    }
   
    });

RelativeLayoutto.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View v)
    {
    showDialog(date_dialog_to);
    }
   
    });

}




@Override
    protected Dialog onCreateDialog(final int id) {
        Dialog dialog = null;
      
        calendar = Calendar.getInstance();
            dialog = new DatePickerDialog(
                this,
                new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker dp, int year,int month, int dayOfMonth) {              
                    switch(id)
                    {
                    case 0:
                    textViewform.setText(year + "-" + (month+1) + "-" + dayOfMonth);
                         break;
                    case 1:
                    textViewto.setText(year + "-" + (month+1) + "-" + dayOfMonth);
                            break;
                    }
                    }
                }, 
                calendar.get(Calendar.YEAR), // 传入年份
                calendar.get(Calendar.MONTH), // 传入月份
                calendar.get(Calendar.DAY_OF_MONTH) // 传入天数
            );
        
        
        return dialog;
    }



@Override 
public boolean onKeyDown(int keyCode, KeyEvent event) { 
   
   if (keyCode == KeyEvent.KEYCODE_BACK) { 
       Bundle bundle = new Bundle(); 
       bundle.putString("datefrom", textViewform.getText().toString()); 
       bundle.putString("dateto", textViewto.getText().toString()); 
       bundle.putString("drug", editTextdrug.getText().toString()); 
       bundle.putString("note", editTextnote.getText().toString()); 
       setResult(RESULT_CANCELED, this.getIntent().putExtras(bundle)); 

       
       this.finish(); 
       return true; 
   }else { 
       return super.onKeyDown(keyCode, event); 
   } 
}
}



你可能感兴趣的:(Android开发)