zoukankan      html  css  js  c++  java
  • 使用 Bundle在Activity间传递数据


    使用    Intent 启动另一个 Activity
    Intent  showNextPage_Intent=new  new  new  new  Intent();
    showNextPage_Intent.setClass(UsingBundel.this    ,NextPageActivity.class);
    startActivity(showNextPage_Intent);

    在多个Activity 之间切换时候,注意每个  Activity 都应在 AndroidManifest.xml AndroidManifest.xml AndroidManifest.xml AndroidManifest.xml  中有所声明定义(如下)
    <application  android:label="@string/app_name"
    android:icon="@drawable/chinazphone" >
    <activity  android:name=".UsingBundel"
    android:label="@string/app_name" >
    <intent-filter >
    <action  android:name="android.intent.action.MAIN"  />
    <category  android:name="android.intent.category.LAUNCHER"  />
    </intent-filter>
    </activity>
    <activity  android:name=".NextPageActivity"
    android:label="@string/nextpage" ></activity>
    </application>
    新的 Activity 在 AndroidManifest.xml 中必须定义声明


    使用 Bundle  在 Activity间传递数据

    从源请求 Activity 中通过一个 Intent  把一个服务请求传到目标 Activity 中
     private  Intent  toNextIntent;//Intent 成员声明
    toNextIntent=new Intent();//Intent 定义
    toNextIntent.setClass(TwoActivityME3.this,  SecondActivity3. class);
    // 设定开启的下一个 Activity
    startActivityForResult(toNextIntent,  REQUEST_ASK);
    // 开启 Intent 时候  ,把请求码同时传递
    在源请求 Activity 中等待 Intent 返回应答结果,通过重载 onActivityResult()方法
    @Override
    protected  void  onActivityResult(int requestCode,
      int  resultCode,  Intent  data)  {
    //  TODO   Auto-generated  method  stub
    super.onActivityResult(requestCode,  resultCode,  data);
    if(requestCode==REQUEST_ASK){
    if(resultCode==RESULT_CANCELED){
    setTitle("Cancel****");
    }else if(resultCode==RESULT_OK){
    showBundle=data.getExtras();// 从返回的 Intent 中获得 Bundle
    Name=showBundle.getString("myName" );// 从 bundle 中获得相应数据
    text.setText("the  name  get  from  the  second  layout:\n"+Name);
    }
    }
    }
    ☻   第一个参数是你开启请求 Intent 时的对应请求码,可以自己定义。
    ☻   第二个参数是目标 Activity 返回的验证结果码
    ☻   第三个参数是目标 Activity 返回的 Intent
    目标 Activity 中发送请求结果代码,连同源 Activity 请求的数据一同绑定到 Bundle中通过 Intent 传回源请求 Activity 中
    backIntent=new  new  new  new  Intent();
    stringBundle=new  new  new  new  Bundle();
    stringBundle.putString("myName",  Name);
    backIntent.putExtras(stringBundle);
    setResult(RESULT_OK,  backIntent);// 返回 Activity 结果码
    finish();


  • 相关阅读:
    【来自知乎】AR技术可以通过H5实现吗?不通过APP
    太虚AR
    【ArUco】- Augmented reality library based on OpenCV
    unity MVC简易框架! MVC in Code Control
    游戏服务器框架与互联网产品的认识
    关于 boost::asio::io_service::run() 出现【句柄无效】的问题
    编译luabind-0.9.1 出现 error C2665: 'boost::operator ==' : none of the 4 overloads could convert all the argument types 的解决方案
    javascript 控制 table tr display block 显示模式时,只对第一个单元格有效
    Ogre::UINT 与 其他库的 类型冲突问题
    排序仿函数 出现 std::priority_queue::push() 的 invalid operator < 异常
  • 原文地址:https://www.cnblogs.com/feisky/p/1745113.html
Copyright © 2011-2022 走看看