码迷,mamicode.com
首页 > 其他好文 > 详细

Activity(2. 跳转)

时间:2015-01-23 21:22:12      阅读:152      评论:0      收藏:0      [点我收藏+]

标签:

Activity的跳转有两种方式:

  • 普通跳转:
Intent intent = new Intent(MainActivity.this, SecondActivty.class);
startActivity(intent);
  • 带返回值的跳转:
    • 调用startActivityForResult,参数带请求索引
public static final int REQUEST_SEC = 1; //请求的ID

Intent backIntent = new Intent(MainActivity.this, SecondActivty.class);
startActivityForResult(backIntent, REQUEST_SEC);
    • 接收端Activity处理该跳转请求
Intent intent = new Intent();
intent.putExtra("value", "hello word");
intent.putExtra("input", MainActivity.REQUEST_SEC);
setResult(RESULT_BACK,intent);
finish();//返回前调用finish,销毁本activity,类似于back
    • 在MainActivity中处理返回
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        
        Log.i(TAG_ACTIVITY, "requestCode:" + requestCode);
        Log.i(TAG_ACTIVITY, "resultCode:"  + resultCode);
        
        if(resultCode == SecondActivty.RESULT_BACK && REQUEST_SEC == requestCode){
            Toast.makeText(getApplicationContext(), data.getStringExtra("value"),
                     Toast.LENGTH_SHORT).show();
        }
    }

 

Activity(2. 跳转)

标签:

原文地址:http://www.cnblogs.com/Fredric-2013/p/4214545.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!