Activity跳转的几种方法

1、显示跳转

Intent it = new Intent(MainActivity.this, AcitivityA.class);
startActivity(it);

一般应用内部跳转会经常使用该方法。

2、隐式跳转

Intent it = new Intent();
it.setAction("com.test.start.action");
startActivity(it);

不需要指定跳转的Activity名字,只需双方协定好指定的action即可,该方法一般常用于外部应用跳转。

3、通过ComponentName跳转

 Intent it = new Intent();
 ComponentName componentName = new ComponentName("com.test.helleworld", "com.test.helloworld.ActivityA");
 it.setComponent(componentName);
 startActivity(it);

通过组件名称跳转需要知道包名和Activity名称,该方法一般用于外部应用跳转。

4、通过包名、类名跳转

Intent it = new Intent();
it.setClassName("com.test.helleworld", "com.test.helloworld.ActivityA");
startActivity(it);

与上述第三种方法类似,都需要知道包名和Activit名称,其实setClassNmae里面也是通过设置ComponentName的,该方法一般用于外部应用跳转。

5、根据包名跳转

 PackageManager pm = getPackageManager();
 Intent it = pm.getLaunchIntentForPackage("com.test.helloworld");
 //it.setAction("android.intent.action.MAIN");
 startActivity(it);

根据应用包名跳转,这里打开的是跳转应用的默认启动Activity。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。