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:
        }
    }

 

 

 

 

你可能感兴趣的:(Android入门篇2-activity调用跟数据传递)