Ahmed Amin
Ahmed Amin

Reputation: 1

Start same Activity from 2 different Activity and updating data

I have three activities.

In the main activity I have 3 buttons. The first and second buttons take me to the other activities. The third button shows me the data I choose from them using an intent.

But I can't see the data from the second and the third activities together. I only see one of them.

I think each of them creating a different main activity. I want just one main activity that sums the data from both the second and the third activities.

Here is my java code for the main activity:

@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);
}

The second activity

@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);
}

The third activity is the same as the second with different images and text.

Upvotes: 0

Views: 87

Answers (1)

Tim
Tim

Reputation: 43314

What you are doing is this:

  • From Main, start Main2, start new instance of Main -- only have data from Main2
  • From Main, start Main3, start new instance of main -- only have data from Main3

What you should be doing is this

  • From Main, start Main2 with startActivityForResult(), do stuff in Main2, end Main2 with setResult() and finish(), store data in Main, repeat for Main3

With setResult() you can pass the data from Main2 back to Main, same goes for Main3.

Upvotes: 1

Related Questions