首页 > 代码库 > Activity(2. 跳转)
Activity(2. 跳转)
Activity的跳转有两种方式:
- 普通跳转:
Intent intent = new Intent(MainActivity.this, SecondActivty.class);startActivity(intent);
- 带返回值的跳转:
- 调用startActivityForResult,参数带请求索引
public static final int REQUEST_SEC = 1; //请求的IDIntent 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. 跳转)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。