Android Intent的几种跳转方式

1、显示跳转

Intent intent=new Intent();
intent.setClass(MainActivity6.this, MainActivity6son1.class);
startActivity(intent);

2、隐式跳转

Intent intent=new Intent();
intent.setAction("aaa.bbb.ccc.ddd");
startActivity(intent);

manifest里需添加action

<activity android:name=".sonActivities.MainActivity6son1">
    <intent-filter>
        <action android:name="aaa.bbb.ccc.ddd" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

3、简单的传值

发起者:

Intent intent=new Intent(MainActivity6.this,MainActivity6son1.class);
if(!editText.getText().toString().trim().equals("")){
    intent.putExtra("info",editText.getText().toString().trim());//通过putExtra传递
}
startActivity(intent);

接收者:

String info=getIntent().getStringExtra("info");

4、通过bundle传值

传递:

        Intent intent = new Intent();
        Bundle bundle=new Bundle();
        bundle.putString("table_id",this.getIntent().getStringExtra("table_id"));
        bundle.putString("table_no",this.getIntent().getExtras().getString("table_no"));
        bundle.putSerializable("listSelectFoods", (Serializable) listSelectFoods);
        intent.putExtras(bundle);       //把Bundle放进intent中
        intent.setClass(OrderMealAty2.this, ConfirmOrderMealAty.class);
        startActivity(intent);

接收:

        Bundle bundle=this.getIntent().getExtras();
        String table_id =bundle.getString("table_id");
        String table_no = bundle.getString("table_no");
        List<FoodByMenu> listSelectFoods= bundle.getSerializable("listSelectFoods");

5、跳转到网站

Uri uri=Uri.parse("http://www.baidu.com");
Intent intent=new Intent(Intent.ACTION_VIEW,uri);
//intent.setAction("android.intent.action.VIEW");
startActivity(intent);

6、数据回传

传输数据:startActivityForResult(intent,传递的请求数字自定义)

Intent intent = new Intent(MainActivity6.this, MainActivity6son1.class);
intent.putExtra("hello", "你好吗?");
startActivityForResult(intent, 1000);

start的activity需要添加这个方法,用来接收返回数据:

@Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode==1000 && resultCode==1001){
            Toast.makeText(this, "数据返还:" + data.getStringExtra("back"), Toast.LENGTH_SHORT).show();
        }

    }

接收与回传:setResult(回传的数字自定义,intent)

Intent intent=new Intent(MainActivity6son1.this, MainActivity6.class);
intent.putExtra("back","返还数据!!!");
setResult(1001,intent);
finish();

上一篇:ESP32 sample code 编译报错: FAILED: esp-idf/mbedtls/x509_crt_bundle


下一篇:Android跳转新页面,并在新页面关闭后传值给本页面,onActivityResult 方法实现