Communication between activity and activity

Source: Internet
Author: User

Activation of communication activity between activity and activity

The simplest way for an activity to start another activity is to use the startactivity (Intent Intent) method, This method call request is sent to the operating system Activitymanager responsible for creating the activity instance and calling its Oncreat () method. Activitymanager is the Intent parameter that passes through the startactivity (Intent Intent) method to know which activity to start.

Activitymanager maintains a fallback stack that is exclusive to a non-specific application. The fallback stack is shared by all apps ' activity. This is one of the reasons why Activitymanager is designed as an activity manager at the operating system level to be responsible for initiating the application activity. Not limited to individual applications, the fallback stack is shared with the operating system and devices as a whole.

Description: Return information to Demo2activity via demo2activity startup demo2_1activity or demo2_2activity,demo2_1activity or demo2_2activity exit

  1. In the Demo2activity

    Intent intent = new Intent(Demo2Activity.this, Demo2_1Activity.class);intent.putExtra("Demo2Activity", "Hello,Demo2_1Acitvity,this is Demo2Activity");    startActivityForResult(intent, 0);//requestCode必须大于等于0,否则onActivityResult()方法不会执行//如果在Demo2_1Acitvity中没有返回消息,那么resultCode会等于requestCode,并且data = null

    In this way, we pass the message to demo2_1acitvity.

  2. In the Demo2_1acitvity

    /*收到 Demo2Activity 的消息*/Intent intent1 = getIntent();Log.d("demoinfo", "Demo2_1Activity收到:  " + intent1.getStringExtra("Demo2Activity"));/*给Demo2Activity发送消息*/Intent intent2 = new Intent();intent2.putExtra("Demo2_1Activity", "I‘m Demo2_1Activity!!");//setResult(1); 两种方式,这个是只传一个resultCodesetResult(1, intent2);
  3. overloading Onactivityresult (int requestcode, int resultcode, Intent data) method in Demo2activity

    requestCode就是之前在Demo2Activity中startActivityForResult(Intent, int)传入的requestCoderesultCode就是在Demo2_1Acitvity中setResult(int, Intent);传入的int resultCodedata就是在Demo2_1Acitvity中setResult(int, Intent);传入的Intent

Code:

Package Com.ashzheng.studydemo.demo2;import Android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.util.log;import Android.view.view;import Android.widget.button;import    Com.ashzheng.studydemo.r;public class Demo2activity extends Activity {private Button bt1;    Private Button BT2;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.LAYOUT.ACTIVITY_DEMO2);        BT1 = (Button) Findviewbyid (R.ID.DEMO2_BT1);        BT2 = (Button) Findviewbyid (R.ID.DEMO2_BT2);                Bt1.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {                Intent Intent = new Intent (demo2activity.this, Demo2_1activity.class);                Intent.putextra ("Demo2activity", "Hello,demo2_1acitvity,this is Demo2activity");//startactivity (intent); Startactivityforresult (Intent, 0);//requestcode must be greater than or equal to0, otherwise the Onactivityresult () method will not execute}});                Bt2.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) {                Intent Intent = new Intent (demo2activity.this, Demo2_2activity.class);                Intent.putextra ("Demo2activity", "Hello,demo2_2acitvity,this is Demo2activity");//startactivity (intent);    Startactivityforresult (Intent, 0);//requestcode must be greater than or equal to 0, otherwise the Onactivityresult () method will not execute}}); } @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {Super.onactivi        Tyresult (Requestcode, ResultCode, data);        LOG.D ("Demoinfo", "Requestcode =" + Requestcode);        LOG.D ("Demoinfo", "ResultCode =" + ResultCode);            if (null = = data) {LOG.D ("Demoinfo", "data = = null");        Return } switch (ResultCode) {case 1:LOG.D ("Demoinfo", Data.getstringextra ("DemO2_1activity "));            Break                Case 2:LOG.D ("Demoinfo", Data.getstringextra ("demo2_2activity"));        Break }    }}
  Package Com.ashzheng.studydemo.demo2;import android.app.activity;import Android.content.intent;import Android.os.bundle;import Android.util.log;import Com.ashzheng.studydemo.r;public class Demo2_1Activity extends         Activity {@Override protected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_demo2_1);        Intent intent1 = Getintent ();        LOG.D ("Demoinfo", "Demo2_1activity Received:" + Intent1.getstringextra ("demo2activity"));        Intent Intent2 = new Intent ();        Intent2.putextra ("Demo2_1activity", "I ' m demo2_1activity!!");        Setresult (1);    Setresult (1, Intent2); }}
package com.ashzheng.studydemo.demo2;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import com.ashzheng.studydemo.R;public class Demo2_2Activity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_demo2_2);        Intent intent1 = getIntent();        Log.d("demoinfo", "Demo2_2Activity收到:  " + intent1.getStringExtra("Demo2Activity"));        Intent intent2 = new Intent();        intent2.putExtra("Demo2_2Activity", "I‘m Demo2_2Activity!!");        //setResult(2);        setResult(2, intent2);    }}

Refer to the "Android Programming Authority Guide"

Communication between activity and activity

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.