1. 程式人生 > >Android中Intent傳值的幾種方法

Android中Intent傳值的幾種方法

1.使用putextra

  


                Intent intent = new Intent();
                intent.putExtra("test","asdf");
                intent.setClass(MainActivity.this,Demo1Activity.class);
                startActivity(intent);

第二個頁面接收 :

 String stringExtra = getIntent().getStringExtra("test");

注意:這種方式僅限於在同進程中傳遞值,傳遞物件:

https://blog.csdn.net/afdasfggasdf/article/details/83069899

2.使用bundle傳值

  這裡我們看下bundle的原始碼  繼承baseBundle 

public final class Bundle extends BaseBundle implements Cloneable, Parcelable

 

 public void putByte(@Nullable String key, byte value) {
        super.putByte(key, value);
    }

其中的put方法都是呼叫父親的put的方法

檢視父親的方法 實質就是一個arraylist

void putShort(@Nullable String key, short value) {
        unparcel();
        mMap.put(key, value);
    }
  ArrayMap<String, Object> mMap = null;
傳值接收:
 Bundle bundle = new Bundle();
                bundle.putString("test","asdf");

                intent.putExtras(bundle);
 Bundle bundle = getIntent().getExtras();
        String test = (String) bundle.get("test");