首页 > 代码库 > android intent 跳转

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