开发者

Eclipse, Android, 2.2, Attempting to pass data between Activities and then place data in a email intent

I am attempting to pass data from Activity B back to Activity A and then throug开发者_如何转开发h a button with an email Intent (on Activity A) I would like to add the data from Activity B to the text of the email. this is what I have so far:

Activity B

    String fireinvolvedsave;
EditText FIinvolvedtext;        

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.firescreen);

    FIinvolvedtext = (EditText) findViewById(R.id.FIinvolvedtext);
    fireinvolvedsave = FIinvolvedtext.getText().toString();

    Intent pass = new Intent(getApplicationContext(), Main.class);

    pass.putExtra("Involved", fireinvolvedsave);
    startActivity(pass);

Activity A

    public void onClick(View v) {
    switch(v.getId()){
    case R.id.EmailStart:
        Bundle extras = getIntent().getExtras(); 
        if(extras !=null) {}
        Intent EmailSend = new Intent(android.content.Intent.ACTION_SEND);
        EmailSend.setType("plain/text");
        EmailSend.putExtra(Intent.EXTRA_SUBJECT, "Fire");
        EmailSend.putExtra(android.content.Intent.EXTRA_TEXT,

                "Involved: "+extras.getString("Involved"));


    break;}}}

When I run this on my device (HTC EVO 2.2) The email comes up properly; however the data from the EditText (FIinvolvedtext) is not there. Could anyone help me see what I am missing?


Ok, well, you almost have the answer already.

I believe that the problem lies in where you are getting the intent to look for the extras. my example below works perfectly.

Keep in mind that adding FLAG_SINGLE_TOP to the intent is the way to make sure that the second activity that you are getting is not a reissue of one that is already there, and Act2.class has its Launch mode (in the AndroidManifest.xml) set to singleTop.

public class Main extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button.setOnClickListener( new OnClickListener(
             @Override
             public void onClick(View v){
                 startActivity(new Intent(this, Act2.class).putExtra("passed", "Here is the passed text").addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP));
             }
        );
    }
}

and the second activity

public class Act2 extends Activity {
    @Override
    protected void onResume() {
        super.onResume();
        Intent i = getIntent();
        if(i.getExtras()!=null && i.getExtras().containsKey("passed")){
            Intent sendEmail = new Intent(android.content.Intent.ACTION_SEND);
            sendEmail.setType("plain/text");
            sendEmail.putExtra(Intent.EXTRA_SUBJECT, "Passing Data");
            sendEmail.putExtra(android.content.Intent.EXTRA_TEXT,
                "Passed Data: "+ i.getExtras().getString("passed"));
            startActivity(Intent.createChooser(sendEmail, "Send mail..."));
        }
    }
}

This all works fine. Hope this answers your question.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜