从2个不同的Activity启动相同的Activity并更新数据

时间:2015-10-26 14:31:39

标签: java android android-intent

我有三项活动。

在主要活动中我有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);
}

第三项活动与第二项活动相同,不同的图像和文字。

1 个答案:

答案 0 :(得分:1)

你在做什么是这样的:

  • Main开始Main2,启动<{1}}的实例 - 仅包含来自Main的数据
  • Main2开始Main,启动<{1}}的实例 - 仅包含来自Main3的数据

你应该做的是这个

  • main开始,使用Main3开始Main,在Main2中填写内容,使用startActivityForResult()Main2结束Main2 ,将数据存储在setResult()中,重复finish()

使用Main,您可以将数据从Main3传递回setResult(),同样适用于Main2