Android Activity Parameter Pass

Source: Internet
Author: User

The Android activity parameter passed through the online data collection finally tested successfully, can be passed between two activity parameters. Post some code first (the key code is commented), for reference only.

The first of the Activity:activity_main

Importing related Packages

Import Android.os.Bundle;
Import Android.view.Menu;
Import Android.view.MenuItem;
Import Android.view.View;
Import Android.view.View.OnClickListener;
Import Android.util.Log;
Import Android.widget.Button;
Import Android.widget.TextView;
Import android.content.Intent;
Import android.app.Activity;


button to pass parameters:

Private Button BT;
Private TextView TW1;

protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
bt= (Button) Findviewbyid (r.id.btnback);
tw1= (TextView) Findviewbyid (R.ID.EDTREC);
Bt.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
Intent it= new Intent ();
Passing parameters
It.putextra ("Name", Tw1.gettext (). toString ());
It.setclass (Mainactivity.this, Showinfo.class);
StartActivity (IT); Do not receive return values
Startactivityforresult (it, 1);//Receive return value here the parameter must be >0 otherwiseOnactivityresult cannot execute

MainActivity.this.finish (); Terminate activity

}
});
}

Receive return Parameters: The front button must be set to Startactivityforresult (it, 1); Receive return value

/**
* Receive the activity method with return value
*/
@Override
protected void Onactivityresult (int requestcode, int resultcode, Intent data)
{

Super.onactivityresult (Requestcode, ResultCode, data);
LOG.I ("Main", "Requestcode:" +requestcode+ "ResultCode:" +resultcode);
Here Requestcode=1,resultcode=-1
if (Requestcode = = 1) {
Tw1.settext (Data.getstringextra ("return"));
}
Tw1.settext (Data.getstringextra ("return"));

}

A second activity showinfo:

private TextView TV1;
Private Button Btnback;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_show_info);
New page receives data

Intent intent=this.getintent ();
String Name=intent.getstringextra ("name");
LOG.I ("Gets the name value to", name);
TV1 = (TextView) Findviewbyid (R.ID.EDTREC);
Tv1.settext ("The value passed is:" +name);

btnback= (Button) Findviewbyid (r.id.btnback);//define Return button
Btnback.setonclicklistener (New Onclicklistener ()
{

@Override
public void OnClick (View v)//return button pass parameter to main page
{
Intent intent=new Intent ();
Intent.putextra ("Return", Tv1.gettext (). toString ()); Passing parameters to return
Setresult (RESULT_OK, intent);
Finish (); Close Current Activity

}
});

}



This article is from "My site I am the Master" blog, please be sure to keep this source http://clchyj.blog.51cto.com/2379532/1568328

Android Activity Parameter Pass

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.