我使用 bundle 將字符串從我的MainActivity子類型傳遞到我的第二個活動。但我在第三個活動中也需要相同的字符串。我在我的主要活動中使用這樣的代碼 Bundle getGenre_Bundle = new Bundle(); if (genre.equals(selector.Crime)) { Intent i = new Intent(getBaseContext(),Crime.class); getGenre_Bundle.putString("crime",selector.Crime); i.putExtras(getGenre_Bundle); startActivity(i);然后我在我的第二個活動中使用 Bundle p = getIntent().getExtras(); Genre = p.getString("crime");這很好用,但是如果我嘗試在我的第三個活動中調用它,它會在我的日志中返回一個錯誤,即我的firebase child(Genre)cant be null.我已經通過在我的第二個活動中創建一個新的包來解決這個問題,該包重新收集字符串以傳遞給我的第三個活動。但它的做法有點混亂。有沒有更簡單/更好的方法將字符串傳遞給任何活動?
1 回答

拉丁的傳說
TA貢獻1789條經驗 獲得超8個贊
您可以在第二個活動中從之前的活動中獲取整個包,并使用putExtras()簡單地設置意圖
Bundle old = getIntent().getExtras();
Intent thirdActivity = new Intent(this,thirdActivity.class);
thirdActivity.putExtras(p);
添加回答
舉報
0/150
提交
取消