The way Android implements communication between activity _android

Source: Internet
Author: User

The examples in this article describe the way Android implements communication between activity. Share to everyone for your reference. Specifically as follows:

A system-supplied startactivity (Intent Intent) method can be used to open new activity in an activity, and you can decide whether to pass parameters for new activity before opening a new activity.

First: Open new activity, do not pass parameters

public class Mainactivity extends activity {
  @Override public
  void OnCreate (Bundle savedinstancestate) {
    Super.oncreate (savedinstancestate);
    Setcontentview (r.layout.main);
    Button btnopen= (button) This.findviewbyid (r.id.btnopen);
    Btnopen.setonclicklistener (New View.onclicklistener () {public
      void OnClick (View v) {
        //new an explicit intent, The first parameter is the current Activity class object, and the second parameter is the activity class startactivity you want to open
        (new Intent (Mainactivity.this, Otheractivity.class));
      }
    });
  }
}

Second: Open a new activity and pass several arguments to it

Package com.ljq.activitys;
Import android.app.Activity;
Import android.content.Intent;
Import Android.os.Bundle;
Import Android.view.View;
Import Android.widget.Button; public class Mainactivity extends activity {@Override public void onCreate (Bundle savedinstancestate) {Super.onc
    Reate (savedinstancestate);
    Setcontentview (R.layout.main);
    Button btnopen= (button) This.findviewbyid (R.id.btnopen); Btnopen.setonclicklistener (New View.onclicklistener () {public void OnClick (View v) {//First: Open a new activity, do not pass the parameter Number//Create an explicit intent, the first parameter is the current Activity class object, and the second parameter is the activity class you want to open//startactivity (new Intent (Mainactivity.this, Othera
        Ctivity.class));
        Second: Open new activity and pass several parameters to it Intent intent=new Intent (Mainactivity.this, Otheractivity.class);
        The Bundle class is used for carrying data Bundle bundle=new Bundle ();
        Bundle.putstring ("name", "Linjiqin");
        Bundle.putint ("Age", 24);
        Attached with additional data Intent.putextras (bundle); StartactIvity (Intent);
  }
    });

 }
}

To receive the parameters passed by the previous activity in a new activity

Package com.ljq.activitys;
Import android.app.Activity;
Import Android.os.Bundle;
Import Android.util.Log;
public class Otheractivity extends activity {
  Private final static String tag= "Otheractivity";
  @Override
  protected void onCreate (Bundle savedinstancestate) {
    super.oncreate (savedinstancestate);
    Setcontentview (r.layout.other);
    In the new activity receive the parameters passed by the preceding activity
    Bundle bundle=this.getintent (). Getextras ();
    String name=bundle.getstring ("name");
    Integer age=bundle.getint ("Age");
    LOG.I (TAG, name+ ":" +age);
  }


I hope this article will help you with your Android program.

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.