Activity跳转的几种方式

来源:互联网 发布:ubuntu翻译软件 编辑:程序博客网 时间:2024/06/05 16:21

今天来说说Activity的跳转的三种方式:1. 显示跳转;2. 隐式跳转;3. ComponentName方式跳转。


一、显示跳转

Intent intent2 = new Intent(MainActivity.this,SecondActivity.class);                startActivity(intent2);

二、隐式跳转

1. java代码

Intent intent3 = new Intent();intent3.setAction("com.czj.yinshi");tartActivity(intent3);

2. AndroidMainifest.xml配置

 <activity android:name=".YinshiActivity">            <intent-filter>                <action android:name="com.czj.yinshi"/>                <category android:name="android.intent.category.DEFAULT"/>            </intent-filter> </activity>

三、ConponentName方式跳转

Intent intent1 = new Intent();intent1.setComponent(new ComponentName("com.czj.componentnamedemo",                        "com.czj.componentnamedemo.SecondActivity"));startActivity(intent1);



下面是具体的代码:

1. MainActivity.java


package com.czj.componentnamedemo;import android.content.ComponentName;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;public class MainActivity extends AppCompatActivity implements View.OnClickListener {    private Button button1,button2,button3;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        button1 = (Button) findViewById(R.id.btn1);        button2 = (Button) findViewById(R.id.btn2);        button3 = (Button) findViewById(R.id.btn3);        button1.setOnClickListener(this);        button2.setOnClickListener(this);        button3.setOnClickListener(this);    }    @Override    public void onClick(View v) {        switch (v.getId()) {            case R.id.btn1: //ComponentName方式跳转                Intent intent1 = new Intent();
//参数一:包名   参数二:SecondActivity 的全包名                intent1.setComponent(new ComponentName("com.czj.componentnamedemo",                        "com.czj.componentnamedemo.SecondActivity"));                startActivity(intent1);                break;            case R.id.btn2:                Intent intent2 = new Intent(MainActivity.this,SecondActivity.class);                startActivity(intent2);                break;            case R.id.btn3:                Intent intent3 = new Intent();                intent3.setAction("com.czj.yinshi");                startActivity(intent3);                break;            default:                break;        }    }}


2. AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"          package="com.czj.componentnamedemo">    <application        android:allowBackup="true"        android:icon="@mipmap/ic_launcher"        android:label="@string/app_name"        android:supportsRtl="true"        android:theme="@style/AppTheme">        <activity android:name=".MainActivity">            <intent-filter>                <action android:name="android.intent.action.MAIN"/>                <category android:name="android.intent.category.LAUNCHER"/>            </intent-filter>        </activity>        <activity android:name=".SecondActivity"/>        <activity android:name=".YinshiActivity">            <intent-filter>                <action android:name="com.czj.yinshi"/>                <category android:name="android.intent.category.DEFAULT"/>

</intent-filter> </activity> </application></manifest>

//隐式跳转要注意下面 
<action android:name="com.czj.yinshi"/><category android:name="android.intent.category.DEFAULT"/>


0 0
原创粉丝点击