The NoteBook of EricKong

            BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
            611 Posts :: 1 Stories :: 190 Comments :: 0 Trackbacks

          Intent用法實(shí)例

          1.無參數(shù)Activity跳轉(zhuǎn)

          Intent it = new Intent(Activity.Main.this, Activity2.class);
          startActivity(it);   

          2.向下一個Activity傳遞數(shù)據(jù)(使用Bundle和Intent.putExtras)

          Intent it = new Intent(Activity.Main.this, Activity2.class);
          Bundle bundle=new Bundle();
          bundle.putString("name", "This is from MainActivity!");
          it.putExtras(bundle);       // it.putExtra(“test”, "shuju”);
          startActivity(it);            // startActivityForResult(it,REQUEST_CODE);

          對于數(shù)據(jù)的獲取可以采用:

          Bundle bundle=getIntent().getExtras();
          String name=bundle.getString("name");

          3.向上一個Activity返回結(jié)果(使用setResult,針對startActivityForResult(it,REQUEST_CODE)啟動的Activity)

                  Intent intent=getIntent();
                  Bundle bundle2=new Bundle();
                  bundle2.putString("name", "This is from ShowMsg!");
                  intent.putExtras(bundle2);
                  setResult(RESULT_OK, intent);
          4.回調(diào)上一個Activity的結(jié)果處理函數(shù)(onActivityResult)
          @Override
              protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                  // TODO Auto-generated method stub
                  super.onActivityResult(requestCode, resultCode, data);
                  if (requestCode==REQUEST_CODE){
                      if(resultCode==RESULT_CANCELED)
                            setTitle("cancle");
                      else if (resultCode==RESULT_OK) {
                           String temp=null;
                           Bundle bundle=data.getExtras();
                           if(bundle!=null)   temp=bundle.getString("name");
                           setTitle(temp);
                      }
                  }
              }

          posted on 2012-02-29 17:29 Eric_jiang 閱讀(676) 評論(0)  編輯  收藏 所屬分類: Android
          主站蜘蛛池模板: 三门县| 临高县| 桦甸市| 尼玛县| 江西省| 吴桥县| 祁阳县| 嘉峪关市| 泰来县| 仁化县| 屯留县| 集贤县| 绥化市| 英超| 闻喜县| 高唐县| 肇庆市| 双辽市| 漳平市| 许昌县| 关岭| 策勒县| 治多县| 绥阳县| 襄城县| 石家庄市| 新营市| 永寿县| 西充县| 富裕县| 青海省| 永靖县| 察雅县| 鸡泽县| 湛江市| 西宁市| 柞水县| 孟村| 九龙城区| 大港区| 东丰县|