我想创建一个Android应用程序,它有多个活动,我传入数据然后将其发送到MainActivity。我想通过创建意图来传递数据,然后使用savedInstanceState存储它。 如果我通过InformationA但是当我发送数据InfomationB与另一个意图时,它会很好地运行存储的信息。 我也尝试过使用共享首选项。
我如何解决这个问题以及为什么要删除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);
}
});
}
}
答案 0 :(得分:0)
我想你可以尝试一个数组。将每条相关信息放在数组中的特定位置(应该在具有意图的活动之间转移),并且还发送具有意图的那条信息的索引,以便接收活动知道访问它的位置。
答案 1 :(得分:0)
当您声明意图时,可以将Bundle中的参数从Activity传递到另一个Activity。
参见示例:
Intent intent = new Intent(this, AnotherActivity.class);
intent.putExtra("argKey", argValue);
但请注意,Bundle最多只能存储1MB,如果我没有弄错,那么当你需要传递一个巨大的数组时,尝试不同的方法,比如SharedPreferences或保存你想要保存的数据在DataManager单例类中,并在需要时检索它。