android intent 跳转

转自:http://blog.sina.com.cn/s/blog_7309444701014u2d.html

一、不需要返回值的跳转

Intent intent=new Intent();

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

startActivity(intent);

 

二、需要返回值的跳转

Intent intent=new Intent();

intent.setClass(发出请求的acitivy.this, 目标activity.class);

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

状态值作为接收目标返回值的验证。目标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);      
    }

requestCode 接收返回的状态值

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

    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();       
    }
}

android intent 跳转,布布扣,bubuko.com

android intent 跳转

上一篇:android拦截短信并屏蔽系统的Notification


下一篇:web项目的两个创建形式website和webapplication(转)