0

I m making parking navigation system. i have made arrays for garages and pass it into other activity of parking now there are changes in that activity and i have to pass it back to garage activity.Need a simpler solution. thanks in advance

Here is code, I've tried:

GarageActivity

int[] g1 = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int[] g2 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
int[] g3 = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int[] g4 = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
int[] g5 = {0, 0, 1, 1, 1, 1, 1, 1, 1, 1};
int[] g6 = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};

b1.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick (View v){
        Intent it = new Intent(Start.this, MainActivity.class);
        it.putExtra("g1", g1);
        startActivity(it);
    }
 });

ParkingActivity

public void onClick(View v) {
        if (tx1Count == 0) {
            v.setBackgroundResource(R.color.redColor);
            tx1Count = 1;
            b[0] = 1;
            Toast.makeText(MainActivity.this, "Parking Alloted", Toast.LENGTH_SHORT).show();
        } else if (tx1Count == 1) {
            v.setBackgroundResource(R.color.Green);
            tx1Count = 0;
            b[0] = 0;
        }
    }
});
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • 2
    Where did you declare these arrays? Post your code. –  Jul 25 '18 at 13:13
  • b1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent it = new Intent(Start.this, MainActivity.class); it.putExtra("g1", g1); startActivity(it); } }); – Naveed Malik Jul 25 '18 at 13:19
  • int [] g1={1,1,1,1,1,1,1,1,1,1}; int [] g2={0,0,0,0,0,0,0,0,0,0}; int [] g3={1,1,1,1,1,1,1,1,1,1}; int [] g4={1,1,1,1,1,1,1,1,1,1}; int [] g5={0,0,1,1,1,1,1,1,1,1}; int [] g6={1,1,1,1,1,1,1,1,1,1}; – Naveed Malik Jul 25 '18 at 13:20
  • this was the garage activity – Naveed Malik Jul 25 '18 at 13:20
  • 1
    Comments is not the place to post code. Edit your answer and put there any code needed, including the declarations and make sure the reader can understand the scope of the declarations –  Jul 25 '18 at 13:22
  • public void onClick(View v) { if (tx1Count == 0){ v.setBackgroundResource(R.color.redColor); tx1Count = 1; b[0]=1; Toast.makeText(MainActivity.this, "Parking Alloted", Toast.LENGTH_SHORT).show(); } else if (tx1Count == 1){ v.setBackgroundResource(R.color.Green); tx1Count = 0; b[0]=0; } } }); // Parking activity – Naveed Malik Jul 25 '18 at 13:23
  • Please post some code – AbhayBohra Jul 25 '18 at 13:38
  • Possible duplicate of [How to manage \`startActivityForResult\` on Android?](https://stackoverflow.com/questions/10407159/how-to-manage-startactivityforresult-on-android) – Marcos Vasconcelos Jul 25 '18 at 13:45

1 Answers1

0

You will have to change your method to start new activity. First of all you have to start it like this:

private int request_code = 10
startActivityForResult(createIntentWithData(yourList,request_code)

Next your garages have to implement Parcelize to put it into bundle of created intent.

In your first activity you have to override onActivityResult method, and retrieve there data.

To place data in your second activity where it will be updated you can use something like this:

@Override
public void onBackPressed() {
    Intent data = new Intent();
    data.putExtra("key", yourDataHere);
    setResult(Activity.RESULT_OK, data);
    super.onBackPressed();
}
Ikazuchi
  • 433
  • 2
  • 12