android如何从一个应用跳转到另一个页面

众所周知,在一个APP内部,从一个页面跳转到另外一个页面是使用startactivity函数来实现的。

同样的,对于应用之间的跳转也是如此的。应用直接的跳转分为三种情况,为了方便描述,我加上有两个应用A和B,需求是要从A点击一个按钮,可以跳转到B,同时A要传数据给B,B能够接收并且处理:

1.从A跳转到B的主Activity,代码如下:

     

  1. Intent intent = new Intent(Intent.ACTION_MAIN);   
  2.                 intent.addCategory(Intent.CATEGORY_LAUNCHER);   
  3.                 ComponentName comp = new ComponentName("com.nbg.baby",   
  4.                         "com.nbg.baby.MainActivity");   
  5.                 intent.setComponent(comp);    
  6.   
  7.                 int launchFlags = Intent.FLAG_ACTIVITY_NEW_TASK   
  8.                 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED;   
  9.   
  10.                 intent.setFlags(launchFlags);   
  11.   
  12.                 intent.setAction("android.intent.action.VIEW");   
  13.   
  14.                 Bundle bundle = new Bundle();   
  15.                 bundle.putString("from""来自测试应用");   
  16.                 intent.putExtras(bundle);   
  17.                 startActivity(intent);  

    1.1如果B是应用是关闭的,那么在onCreate()函数中增加如下语句:

  1. Bundle bundle = this.getIntent().getExtras();  
  2.          if(bundle!=null && bundle.getString("from")!=null){  
  3.             Util.showToast(s_instance, bundle.getString("from"), Toast.LENGTH_LONG);  
  4.          }  

    1.2 如何B应用是开启状态,那么在onRestart()函数中增加如下语句:

  1. Bundle bundle = this.getIntent().getExtras();  
  2.          if(bundle!=null && bundle.getString("from")!=null){  
  3.             Util.showToast(s_instance, bundle.getString("from"), Toast.LENGTH_LONG);  
  4.          }  

2.从A跳转到B的某个被设置为exported的activity,B应用的状态无关紧要,代码如下:

  1. <span style="white-space:pre">        </span>ComponentName comp = new ComponentName("com.nbg.baby",   
  2.                     "com.nbg.baby.ApiLoginActivity");   
  3.                       
  4.         Intent intent = new Intent();  
  5.         intent.setComponent(comp);     
  6.         intent.setAction(Intent.ACTION_VIEW);   
  7.         Bundle bundle = new Bundle();   
  8.         bundle.putString("extra""我来自星星");   
  9.         intent.putExtras(bundle);  
  10.         context.startActivityForResult(intent, 1);  
  11.          
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值