[英]How to go from second activity to third activity or fourth activity based on first activity data (Radio button clicked)?
我有四個活動,例如Java文件-activity1.java,activity2.java,activity3.java,activity4.java和xml文件-activity_1.xml,activity_2.xml,activity_3.xml,activity_4.xml
現在處於activity1中-我有一個帶有兩個單選按鈕的單選按鈕,還有一個轉到activity2的按鈕
現在在活動2中-我有一個按鈕可以轉到活動3或活動4,這取決於在活動1中單擊了哪個單選按鈕。
我可以刪除活動2並使用如果活動1中的條件轉到活動3或4,但我絕對需要活動2
我對捆綁包,共享的首選項不熟悉
怎么樣? 這方面的任何幫助
您可以只使用意圖並在活動之間傳遞值。 在第一個活動中:
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("someName", valueOfRadioButton);
startActivity(intent);
在下一個活動的onCreate
中,您可以檢索通過以下方式傳遞的值(對於Integer):
getIntent().getIntExtra("someName", someDefaultValue);
現在,您有一個要查詢的變量。 根據值,您可以啟動任何您喜歡的活動。
采用
Intent startSecondActivityIntent = new Intent(FirstActivity.this,SecondActivity.java);
Intent.putExtra("nameOfValue",value);
startActivity(startSecondActivityIntent);
然后,您可以獲得以下值:
getIntent().getIntExtra("nameOfValue",defaultValue);
或者您可以使用SharedPreferences
SharedPreferences sp = getSharedPreferences("nameOfPreferences",MODE_PRIVATE);
SharedPreferences.Editor editor = sp.edit();
editor.putString("nameOfValue",value);
editor.commit();
並在其他活動中獲得此值:
SharedPreferences sp = getSharedPreferences("nameOfPreferences",MODE_PRIVATE);
String value = sp.getString("nameOfValue",defaultValue);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.