Intent 傳遞對象

方法:

可以讓這個要傳遞的對象所屬類實現Serializable或者Parcelable接口,

然後利用onCreate函數中的Bundle參數作爲載體,傳遞這個對象。

例如:

<span style="font-size:14px;color: rgb(75, 75, 75);">
        @Override 
        public void onItemClick(AdapterView<?> parent, View view, int position, long i) 
        { 
            User user= (User) Connect.getUser(position); 
            Intent intent = new Intent(Activity1.this,Activity2.class); 
            Bundle bundle = new Bundle(); 
            bundle.putSerializable("USER", user); 
            intent.putExtras(bundle); 
            startActivity(intent); 
    
        } 
    }


 

 

在接收對象的地方:

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.detail); 
    User </span><span style="color: rgb(75, 75, 75); font-size: 13px; font-family: georgia, verdana, Arial, helvetica, sans-seriff;">user</span><span style="color: rgb(75, 75, 75); font-size: 13px; font-family: georgia, verdana, Arial, helvetica, sans-seriff;">= (Item) getIntent().getSerializableExtra("user"); </span><span style="font-size:14px;color: rgb(75, 75, 75);">
    //這裏就得到user對象了,注意:必須確保user</span><span style="font-family: georgia, verdana, Arial, helvetica, sans-seriff; line-height: 19.5px;"><span style="font-size:12px;color:#666666;">類實現Serializable或者Parcelable接口</span></span><span style="font-size:14px;color: rgb(75, 75, 75);">
}</span>


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章