android intent 跳轉

來源:互聯網
上載者:User

標籤:android   class   blog   code   http   tar   

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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.