我用魔杖創建了一個Android應用程序,該應用程序有多個活動,我傳入數據然後發送給MainActivity。我希望通過創建意圖來傳遞數據然後使用savedInstanceState存儲它。 如果我通過了InformationA,但是當我發送數據InfomationB與另一個意圖存儲的信息A被刪除時,它會很好地工作。 我也嘗試使用共享首選項。如何通過活動發送數據並保存,以備後用?
我該如何解決這個問題,爲什麼是InformationA擦除?
public class MainActivity extends AppCompatActivity {
String order = "Order \n";
String Order1;
String Order2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView warzywa = (TextView) findViewById(R.id.warzywa);
warzywa.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick (View View){
Intent idź_w = new Intent(MainActivity.this, vegetables_activity.class);
startActivity(idź_w);
}
});
TextView owoced = (TextView) findViewById(owoce);
owoced.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View View){
Intent idź_w = new Intent(MainActivity.this, Owoce.class);
startActivity(idź_w);
}
});
Button wysyłka = (Button) findViewById(R.id.zamówienie_całkowite);
wysyłka.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick (View View){
if (Order1==null && getIntent().getExtras().getString("WhatO")!=null)
Order1 = getIntent().getExtras().getString("WhatO");
if (Order2==null && getIntent().getExtras().getString("WhatV")!=null)
Order2 = getIntent().getExtras().getString("WhatV");
order = order + Order1 + Order2;
Toast.makeText(MainActivity.this, "" + order , Toast.LENGTH_SHORT).show();
}
});
}
public void onSaveInstanceState(Bundle savedInstanceState) {
savedInstanceState.putString("Order1", Order1);
savedInstanceState.putString("Order2", Order2);
super.onSaveInstanceState(savedInstanceState);
}
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
Order1 = savedInstanceState.getString("Order1");
Order2 = savedInstanceState.getString("Order2");
}}
1子活動
public class vegetables_activity extends AppCompatActivity {
String Order = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_warzywa);
final ArrayList<Produkt> vegetables = new ArrayList<>();
vegetables.add(new Produkt("tomatoe", 0));
vegetables.add(new Produkt("onion", 0));
vegetables.add(new Produkt("potatoe", 0));
New_Adapter adapter = new New_Adapter(this, vegetables);
ListView listView = (ListView) findViewById(R.id.activity_vegetables);
listView.setAdapter(adapter);
Button order = (Button) findViewById(R.id.zamówienie_warzywne);
order.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View View) {
for (int z = 0; z < vegetables.size(); z++) {
if (vegetables.get(z).getMquantity() != 0) {
Order = "" + Order + vegetables.get(z).getmNazwa_Warzywa() + " razy " + vegetables.get(z).getMquantity() + "\n";
}
}
Context context = getApplicationContext();
Toast.makeText(context, "OK", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(vegetables_activity.this, MainActivity.class);
intent.putExtra("WhatV", Order);
startActivity(intent);
}
});
}}
2子活動
public class Owoce extends AppCompatActivity {
String Order_Owoce = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_warzywa);
final ArrayList<Produkt> vegetables = new ArrayList<>();
vegetables.add(new Produkt("apple", 0));
vegetables.add(new Produkt("banana", 0));
vegetables.add(new Produkt("peach", 0));
New_Adapter adapter = new New_Adapter(this, vegetables);
ListView listView = (ListView) findViewById(R.id.activity_vegetables);
listView.setAdapter(adapter);
Button order = (Button) findViewById(R.id.zamówienie_warzywne);
order.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View View) {
for (int z = 0; z < vegetables.size(); z++) {
if (vegetables.get(z).getMquantity() != 0) {
Order_Owoce = "" + Order_Owoce + vegetables.get(z).getmNazwa_Warzywa() + " razy " + vegetables.get(z).getMquantity() + "\n";
}
}
Context context = getApplicationContext();
Toast.makeText(context, "OK", Toast.LENGTH_SHORT).show();
Intent intent = new Intent(Owoce.this, MainActivity.class);
intent.putExtra("WhatO", Order_Owoce);
startActivity(intent);
}
});
}
}
顯示相關代碼。 –
您是否將數據存儲到'''Bundle'''中?使用相同的密鑰?這會覆蓋原始數據。請發佈代碼以進一步瞭解。 – AshMenhennett
添加了相關的代碼 – Maciek