[英]ANDROID: Start same Activity from 2 different Activity (different Intent)
[英]Start same Activity from 2 different Activity and updating data
我有三個活動。
在主要活動中,我有3個按鈕。 第一個和第二個按鈕將我帶到其他活動。 第三個按鈕顯示我使用意圖從它們中選擇的數據。
但是我看不到第二個和第三個活動的數據。 我只看到其中之一。
我認為每個人都會創建不同的主要活動。 我只需要一個主要活動來匯總第二個和第三個活動的數據。
這是我主要活動的Java代碼:
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
button = (Button) findViewById(R.id.button2);
button1 = (Button) findViewById(R.id.button3);
button2 = (Button) findViewById(R.id.button4);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
textrecievebox.setText(getIntent().getExtras().getString("jockeyno"));
// Capture button clicks
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent = new Intent(MainActivity.this,
Main2Activity.class);
startActivity(myIntent);
}
});
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// Start NewActivity.class
Intent myIntent1 = new Intent(MainActivity.this,
Main3Activity.class);
startActivity(myIntent1);
}
});
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
public void showdata(View v)
{
ImageView show1= (ImageView)findViewById(R.id.imageView3);
ImageView show2= (ImageView)findViewById(R.id.imageView14);
ImageView show3= (ImageView)findViewById(R.id.imageView15);
ImageView show4= (ImageView)findViewById(R.id.imageView16);
ImageView show5= (ImageView)findViewById(R.id.imageView18);
ImageView show6= (ImageView)findViewById(R.id.imageView19);
ImageView show7= (ImageView)findViewById(R.id.imageView20);
ImageView show8= (ImageView)findViewById(R.id.imageView21);
TextView textrecievebox= (TextView)findViewById(R.id.textView2);
textrecievebox.setText(getIntent().getExtras().getString("jockeyno"));
Bundle bundle=this.getIntent().getExtras();
int pic=bundle.getInt("image");
show1.setImageResource(pic);
Bundle bundle1=this.getIntent().getExtras();
int pic1=bundle1.getInt("image1");
show2.setImageResource(pic1);
Bundle bundle2=this.getIntent().getExtras();
int pic2=bundle2.getInt("image2");
show3.setImageResource(pic2);
Bundle bundle3=this.getIntent().getExtras();
int pic3=bundle3.getInt("image3");
show4.setImageResource(pic3);
Bundle bundle4=this.getIntent().getExtras();
int pic4=bundle4.getInt("image4");
show5.setImageResource(pic4);
Bundle bundle5=this.getIntent().getExtras();
int pic5=bundle5.getInt("image5");
show6.setImageResource(pic5);
Bundle bundle6=this.getIntent().getExtras();
int pic6=bundle6.getInt("image6");
show7.setImageResource(pic6);
Bundle bundle7=this.getIntent().getExtras();
int pic7=bundle7.getInt("image7");
show8.setImageResource(pic7);
}
第二項活動
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
jockey = (EditText)findViewById(R.id.editText2);
Controlpanel = (EditText)findViewById(R.id.editText4);
Detector = (EditText)findViewById(R.id.editText);
Extinghuer = (EditText)findViewById(R.id.editText3);
Pumpspinner=(Spinner)findViewById(R.id.Pumpspinner);
Pumpspinner1=(Spinner)findViewById(R.id.spinner);
Pumpspinner2=(Spinner)findViewById(R.id.spinner2);
Pumpspinner3=(Spinner)findViewById(R.id.spinner3);
Pump=(ImageView)findViewById(R.id.imageView);
ArrayAdapter adapter =ArrayAdapter.createFromResource(this,R.array.Pumps,android.R.layout.simple_spinner_item);
Pumpspinner.setAdapter(adapter);
Pumpspinner.setOnItemSelectedListener(this);
ArrayAdapter adapter1 =ArrayAdapter.createFromResource(this,R.array.controlpanel,android.R.layout.simple_spinner_item);
Pumpspinner1.setAdapter(adapter1);
Pumpspinner1.setOnItemSelectedListener(this);
ArrayAdapter adapter2 =ArrayAdapter.createFromResource(this,R.array.firedetector,android.R.layout.simple_spinner_item);
Pumpspinner2.setAdapter(adapter2);
Pumpspinner2.setOnItemSelectedListener(this);
ArrayAdapter adapter3 =ArrayAdapter.createFromResource(this,R.array.fireextighuer,android.R.layout.simple_spinner_item);
Pumpspinner3.setAdapter(adapter3);
Pumpspinner3.setOnItemSelectedListener(this);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
public void onButtonClickSaveData (View v)
{
Intent mysendIntent = new Intent(Main2Activity.this, MainActivity.class);
mysendIntent.putExtra("jockeyno", jockey.getText().toString());
Bundle bundle=new Bundle();
Bundle bundle1=new Bundle();
Bundle bundle2=new Bundle();
Bundle bundle3=new Bundle();
bundle.putInt("image", R.drawable.jockpypump);
bundle1.putInt("image1", R.drawable.controlpanel1);
bundle2.putInt("image2", R.drawable.detector);
bundle3.putInt("image3", R.drawable.extinguisher1);
mysendIntent.putExtras(bundle);
mysendIntent.putExtras(bundle1);
mysendIntent.putExtras(bundle2);
mysendIntent.putExtras(bundle3);
startActivityForResult(mysendIntent,1);
}
第三項活動與第二項活動相同,只是圖像和文字不同。
您正在執行的操作是這樣的:
Main
啟動Main2
,啟動Main
新實例-僅具有Main2
數據 Main
,啟動Main3
,啟動main
新實例-僅具有Main3
數據 你應該做的是這個
Main
開始,以startActivityForResult()
開始Main2
,在Main2
,以setResult()
和finish()
結束Main2
,將數據存儲在Main
,重復Main3
使用setResult()
您可以將數據從Main2
回Main
, Main3
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.