• android intent 跳转


    一、不需要返回值的跳转

    Intent intent=new Intent();

    intent.setClass(目前的acitivy.this, 目标activity.class);

    startActivity(intent);

    二、需要返回值的跳转

    Intent intent=new Intent();

    intent.setClass(目前的acitivy.this, 目标activity.class);

    startActivity(intent);

    startActivityForResult(intent, 状态值(int类型));

    状态值作为接受目标返回值的验证。

    requestCode 接收返回的状态值

    resultCode :RESULT_OK  RESULT_CANCELED 可以自己赋值,按返回键时,系统默认赋值为:RESULT_CANCELED

    目标activity 关闭才能返回请求activity

    例如:

      1、请求页面

    public class MainActivy extends Activity {
       
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            Intent intent=new Intent();
            intent.setClass(MainActivy.this, ReceiveMSG.class);
            startActivityForResult(intent, 0);      
        }

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            // TODO Auto-generated method stub
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode==0) {
                if (resultCode==RESULT_OK) {
                    Toast.makeText(this, "Send SMS RESULT_OK", Toast.LENGTH_SHORT).show();
                }else if (resultCode==RESULT_CANCELED) {
                    Toast.makeText(this, "result cancel", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this, resultCode, Toast.LENGTH_SHORT).show();
                }
            }else {
                Toast.makeText(this, "requsetcode not 0", Toast.LENGTH_SHORT).show();
            }
           
        }
    }

    2、接收返回值界面

    public class ReceiveMSG extends Activity{

        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);        
            ReceiveMSG.this.setResult(RESULT_OK);
            this.finish();       
        }
    }

  • 相关阅读:
    Linux 学习之DNS服务器
    Windows系统镜像自动添加驱动程序
    Linux下集群的搭建
    Heartbeat+LVS构建高可用负载均衡集群
    Keepalived高可用集群搭建(转载linuxIDC)
    CentOS 7.x设置自定义开机启动,添加自定义系统服务
    代码改变世界
    Vim食用指南
    Hibernate三种状态详解
    MySQL服务器的安装与配置
  • 原文地址:https://www.cnblogs.com/kevincode/p/3837874.html
Copyright © 2020-2023  润新知