如何在Android上將對象從一個活動傳遞到另一個活動?我需要能夠在我的應用程序中的多個活動中使用一個對象,它需要是同對象。做這件事最好的方法是什么?我嘗試過將對象設置為“公共靜態”,這樣它就可以被其他活動訪問,但出于某種原因,這并沒有切斷它。還有別的辦法嗎?
3 回答

大話西游666
TA貢獻1817條經驗 獲得超14個贊
CustomListing currentListing = new CustomListing();Intent i = new Intent(); Bundle b = new Bundle();b.putParcelable(Constants.CUSTOM_LISTING, currentListing); i.putExtras(b);i.setClass(this, SearchDetailsActivity.class);startActivity(i);
Bundle b = this.getIntent().getExtras();if (b != null) mCurrentListing = b.getParcelable(Constants.CUSTOM_LISTING);

蕪湖不蕪
TA貢獻1796條經驗 獲得超7個贊
class Myclass { int a; class SubClass { int b; }}
MyClass src = new MyClass();Gson gS = new Gson();String target = gS.toJson(src); // Converts the object to a JSON String
Intent i = new Intent(FromActivity.this, ToActivity.class);i.putExtra("MyObjectAsString", target);
String target = getIntent().getStringExtra("MyObjectAsString");MyClass src = gS.fromJson(target, MyClass.class); // Converts the JSON String to an Object
更新
- 3 回答
- 0 關注
- 473 瀏覽
添加回答
舉報
0/150
提交
取消