(1) passing Activity parameters and activity parameters

Source: Internet
Author: User

(1) passing Activity parameters and activity parameters

1. Main Activity, used to start another Activity ()
Public class MainActivity extends Activity {@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
System. out. println ("onCreate ");

FindViewById (R. id. btnStartBAct). setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View view ){
Intent intent = new Intent (MainActivity. this, BActivity. class );
Intent. putExtra ("data", "Hello Henry"); // PASS Parameters
// StartActivity (new Intent (MainActivity. this, BActivity. class ));
StartActivity (intent );
}
});
}

@ Override
Protected void onStart ()
{
Super. onStart ();
System. out. println ("onStrart ");
}
@ Override
Protected void onRestart ()
{
Super. onRestart ();
System. out. println ("onRestart ");
}
@ Override
Protected void onStop ()
{
Super. onStart ();
System. out. println ("onStop ");
}
@ Override
Protected void onResume ()
{
Super. onResume ();
System. out. println ("onResume ");
}
@ Override
Protected void onPause ()
{
Super. onPause ();
System. out. println ("onPause ");
}
@ Override
Protected void onDestroy ()
{
Super. onDestroy ();
System. out. println ("onDestroy ");
}

@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. menu_main, menu );
Return true;
}

@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
// Handle action bar item clicks here. The action bar will
// Automatically handle clicks on the Home/Up button, so long
// As you specify a parent activity in AndroidManifest. xml.
Int id = item. getItemId ();

// Noinspection SimplifiableIfStatement
If (id = R. id. action_settings ){
Return true;
}

Return super. onOptionsItemSelected (item );
}
}
2. Start from Activity by MainActivity
Public class BActivity extends Activity {

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_ B );
System. out. println ("B onCreate ");

Intent intent = getIntent (); // get the Intent passed by MainActivity

TextView TV = (TextView) findViewById (R. id. TV );
TV. setText (intent. getStringExtra ("data"); // call the getStringExtra ("data") method of Intent to retrieve data
}

@ Override
Protected void onStart ()
{
Super. onStart ();
System. out. println ("B onStrart ");
}
@ Override
Protected void onRestart ()
{
Super. onRestart ();
System. out. println ("B onRestart ");
}
@ Override
Protected void onStop ()
{
Super. onStart ();
System. out. println ("B onStop ");
}
@ Override
Protected void onResume ()
{
Super. onResume ();
System. out. println ("B onResume ");
}
@ Override
Protected void onPause ()
{
Super. onPause ();
System. out. println ("B onPause ");
}
@ Override
Protected void onDestroy ()
{
Super. onDestroy ();
System. out. println ("B onDestroy ");
}


@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
// Inflate the menu; this adds items to the action bar if it is present.
GetMenuInflater (). inflate (R. menu. menu_ B, menu );
Return true;
}

@ Override
Public boolean onOptionsItemSelected (MenuItem item ){
// Handle action bar item clicks here. The action bar will
// Automatically handle clicks on the Home/Up button, so long
// As you specify a parent activity in AndroidManifest. xml.
Int id = item. getItemId ();

// Noinspection SimplifiableIfStatement
If (id = R. id. action_settings ){
Return true;
}

Return super. onOptionsItemSelected (item );
}
}
3. Transfer Data Packet Bundle
3.1 In MainActivity

FindViewById (R. id. btnStartBAct). setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View view ){
Intent intent = new Intent (MainActivity. this, BActivity. class );
// Intent. putExtra ("data", "Hello Henry ");

// Transfer data packets
Bundle bundle = new Bundle ();
Bundle. putString ("name", "henry"); // Add data to the data packet
Bundle. putInt ("age", 24 );
Intent. putExtras (bundle); // Add data packets to Intent

// StartActivity (new Intent (MainActivity. this, BActivity. class ));
StartActivity (intent );
}
});
}
3.2 In BActivity
Intent intent = getIntent ();
Bundle bundle = intent. getExtras (); // receives data packets

TextView TV = (TextView) findViewById (R. id. TV );
// TV. setText (intent. getStringExtra ("data "));
TV. setText (String. format ("name = % s, age = % d", bundle. getString ("name"), (int) bundle. get ("age"); // call the getXXX () function of Bundle to obtain data
4. Pass reference
4.1 Use serialization to pass reference classes
Custom class:
Public class User implements Serializable {// use the serialization method provided by java
Private String name;
Private int age;

Public void setName (String name ){
This. name = name;
}

Public void setAge (int age ){
This. age = age;
}

Public String getName (){
Return name;
}

Public int getAge (){
Return age;
}
Public User (String name, int age)
{
This. name = name;
This. age = age;
}
}
Main Activity:
Public class MainActivity extends Activity {

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );

FindViewById (R. id. btnStartBAct). setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View view ){
Intent intent = new Intent (MainActivity. this, BActivity. class );


Intent. putExtra ("user", (Serializable) new User ("henryQL", 24); // Add a custom reference Class Object
StartActivity (intent );
}
});
}

}
Called Activity:
Public class BActivity extends Activity {

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_ B );

Intent intent = getIntent ();
User user = (User) intent. getSerializableExtra ("user"); // get serialized data

TextView TV = (TextView) findViewById (R. id. TV );
TV. setText (String. format ("nmae = % s, age = % d", user. getName (), user. getAge ()));
}
}
4.2 Use the Parcelable interface provided by Android
Public class User implements Parcelable {
Private String name;
Private int age;

Public void setName (String name ){
This. name = name;
}

Public void setAge (int age ){
This. age = age;
}

Public String getName (){
Return name;
}

Public int getAge (){
Return age;
}
Public User (String name, int age)
{
This. name = name;
This. age = age;
}


@ Override
Public int describeContents (){
Return 0;
}

@ Override
Public void writeToParcel (Parcel parcel, int I ){
Parcel. writeString (getName ());
Parcel. writeInt (getAge ());
}

Public static final Parcelable. Creator <User> CREATOR = new Parcelable. Creator <User> () // The name must be "CREATOR ".
{

@ Override
Public User createFromParcel (Parcel parcel ){
Return new User (parcel. readString (), parcel. readInt ());
}

@ Override
Public User [] newArray (int I ){
Return new User [I];
}
};
}

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("onCreate");

findViewById(R.id.btnStartBAct).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(MainActivity.this,BActivity.class);

intent.putExtra("user",new User("Qianlong",24));

//startActivity(new Intent(MainActivity.this,BActivity.class));
startActivity(intent);
}
});
}
}

Public class BActivity extends Activity {

@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_ B );
System. out. println ("B onCreate ");


Intent intent = getIntent ();
User user = intent. getParcelableExtra ("user"); // get the Parcelable object

TextView TV = (TextView) findViewById (R. id. TV );
// TV. setText (intent. getStringExtra ("data "));
// TV. setText (String. format ("name = % s, age = % d", bundle. getString ("name"), (int) bundle. get ("age ")));
TV. setText (String. format ("nmae = % s, age = % d", user. getName (), user. getAge ()));
}
}
 




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.