-1

I have a main activity containing a mannequin (bear with me) and when you click on its head, it opens a new activity in which you can choose the hair style and color to go on the mannequin's head. I would like to be able to copy this selection to the main activity and present the same picture on the manikin. How can I do this?

AlBlue
  • 23,254
  • 14
  • 71
  • 91
Cameron L
  • 86
  • 12
  • Possible duplicate of [How do I pass data between activities on Android?](http://stackoverflow.com/questions/2091465/how-do-i-pass-data-between-activities-on-android) – OneCricketeer May 09 '16 at 17:15
  • I would suggest to keep your spinner list global for your app and get the selected item position from spinner and load that corresponding item from global list as positions will be same – Vivek Mishra May 09 '16 at 17:15

3 Answers3

1

You can set static variables in your MainActivity

like

public static String hairStyle = "";

then from your spinner after you fill it with data

Spinner hairStylesSpinner = (Spinner) findViewById(R.id.hair_styles_spinner);

SpinnerAdapter spinnerAdapter = new SpinnerAdapter(new ArrayList<>(Arrays.asList(getResources().getStringArray(R.array.hair_styles))), "");

hairStylesSpinner.setAdapter(spinnerAdapter);

get the selected and save it in your MainActivity

MainActivity.hairStyle = yourList.get(hairStylesSpinner.getSelectedItemPosition());

0

Something like this: From your hairstyle acitivity.

Intent _intent = new Intent(this, newscreen.class);
Bitmap _bitmap; // your bitmap
ByteArrayOutputStream _bs = new ByteArrayOutputStream();
_bitmap.compress(Bitmap.CompressFormat.PNG, 50, _bs);
i.putExtra("byteArray", _bs.toByteArray());
startActivity(i);

From your mannequin activity.

 if(getIntent().hasExtra("byteArray")) {
   ImageView _imv= new ImageView(this);
   Bitmap _bitmap = BitmapFactory.decodeByteArray(
            getIntent().getByteArrayExtra("byteArray"),0,getIntent().
   getByteArrayExtra("byteArray").length);        
   _imv.setImageBitmap(_bitmap);
}

I took this answer from a similar question here: how to pass images through intent?

Community
  • 1
  • 1
Josh Fischer
  • 439
  • 2
  • 15
0

I achieved this using by intent.putExtra. Heres How:

I created a button to return to the mainActivity:

<Button
    android:id="@+id/backtomain"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/back_arrow"
    android:onClick="backMain"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true" />

The onClick:"backMain" runs backMain in the java file:

public void backMain(View view){
    Intent intent = new Intent (this, MainActivity.class);
    intent.putExtra("hairStyle", hairSpinner.getSelectedItem().toString());
    intent.putExtra("hairColor", hairColor.getSelectedItem().toString());
    startActivity(intent);
}

The selected items on the spinners are sent from the activity and saved as variables in the main activity:

String hairStyle, hairColor;
Bundle hairExtras = getIntent().getExtras();
if(hairExtras!=null){
    hairStyle = hairExtras.getString("hairStyle");
    hairColor = hairExtras.getString("hairColor");
    displayHair();
}
Cameron L
  • 86
  • 12