Android入门篇2-activity调用跟数据传递

一.activity调用

假设ActivityTest调用SecondActivity

1.显示调用

button1.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        String data = "Hello SecondActivitydfdsf";
        Intent intent = new Intent(ActivityTest.this, SecondActivity.class);
        intent.putExtra("extra_data", data);
        startActivity(intent);
    }
});

2.隐式调用

(1)AndroidManifest.xml 中通过action跟category来固定SecondActivity

        <activity android:name=".SecondActivity" >
            <intent-filter>
                <action android:name="com.example.ActivityTest.ACTION_START" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="com.example.ActivityTest.MY_CATEGORY"/>
            </intent-filter>
        </activity>

 

(2)ActivityTest调用

button1.setOnClickListener(new OnClickListener() {
        @Override
public void onClick(View v) {
  Intent intent = new Intent("com.example.activitytest.ACTION_START"); 
  intent.addCategory("com.example.activitytest.MY_CATEGORY");
  startActivity(intent);   }
});

 

3.调用系统服务

button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse("http://www.baidu.com"));
            startActivity(intent);
} });

 

二.activity之间通过intent传值

1.向下一个活动传递数据

ActivityTest---(value)-->SecondActivity

ActivityTest:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_test);
        Button button1 = (Button) findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String data = "Hello SecondActivitydfdsf";
                Intent intent = new Intent(ActivityTest.this, SecondActivity.class);
                intent.putExtra("extra_data", data);
                startActivity(intent);
            }
        });
    }

SecondActivity:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.second_layout);
        Button button2 = (Button) findViewById(R.id.button_2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = getIntent();
                String data = intent.getStringExtra("extra_data");
                Toast.makeText(SecondActivity.this, data, Toast.LENGTH_SHORT).show();
                Log.d("SecondActivity", data);
            }
        });
    }
}

2.向上一个活动返回数据

SecondActivity---(value)-->ActivityTest

SecondActivity:

        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("data_return", "Hello FirstActivity");
                setResult(RESULT_OK, intent);
            }
        });

返回resultcode跟intent对象

ActivityTest:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity_test);
        Button button1 = (Button) findViewById(R.id.button_1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(ActivityTest.this, SecondActivity.class);
                startActivityForResult(intent, 1);
            }
        });
    }

调用过程如下:

startActivityForResult--->SecondActivity -->onActivityResult .

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 1:
                if (resultCode == RESULT_OK) {
                    String returnedData = data.getStringExtra("data_return");
                    Log.d("ActivityTest", returnedData);
                }
                break;
            default:
        }
    }

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值