0

Im new to android and I just realized how .putExtra works. I want to send 2 different images from two different activities into two different imageviews into my Main Activity. I tried to do so but everytime i pick my second image the first one gets replaced by my default image. Here's my code, any help will be appreciated

My Main Activity:

public class MainActivity extends AppCompatActivity {
TextView champ1;
TextView champ2;
private ImageView image;
private ImageView image2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //--INITIALISING IMAGEVIEW--//
    image = (ImageView)findViewById(R.id.champ1);
    image2 = (ImageView)findViewById(R.id.champ2);
    ImageView image3 = (ImageView)findViewById(R.id.vs);

    //--SETTING RESOURCES FROM ANOTHER ACTIVITY--//
    image.setImageResource(getIntent().getIntExtra("myImageResource",R.drawable.random));
    image2.setImageResource(getIntent().getIntExtra("myImageResource2",R.drawable.random));
    image3.setImageResource(R.drawable.vssymbol);
    //--SETTINGS TEXT FROM ANOTHER ACTIVITY--//
    champ1=(TextView) findViewById(R.id.textview1);
    champ1.setText(getIntent().getStringExtra("message"));
    champ2=(TextView)findViewById(R.id.textview2);
    champ2.setText(getIntent().getStringExtra("message2"));


    //--PICK CHAMP BUTTONS--//
    Button champselect1 = (Button)findViewById(R.id.champselect1);
    Button champselect2 = (Button)findViewById(R.id.champselect2);

    //------------------//
    champselect1.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){

                Intent intent = new Intent(MainActivity.this, pickchamp.class);
                startActivity(intent);

        }
    });

    champselect2.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){
            Intent intent = new Intent(MainActivity.this, pickchamp2.class);
            startActivity(intent);
        }
    });
}
}

Here is the first activity that I`d like to get a picture from

public class pickchamp2 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.AppTheme_Dialog);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pickchamp2);
    getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);

    //--DECLARING IMAGEBUTTONS--//
    ImageButton ab = (ImageButton)findViewById(R.id.ab);
    ImageButton av = (ImageButton)findViewById(R.id.av);
    ImageButton ac = (ImageButton)findViewById(R.id.ac);

    //--ONCLICKLISTENERS--//
    ab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent champ2= new Intent(pickchamp2.this,MainActivity.class);
            champ2.putExtra("myImageResource2", R.drawable.ab);
            champ2.putExtra("message2", "ab");
            startActivity(champ2);
        }
    });
    av.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent champ2= new Intent(pickchamp2.this,MainActivity.class);
            champ2.putExtra("myImageResource2", R.drawable.av);
            champ2.putExtra("message2", "av");
            startActivity(champ2);
        }
    });
    ac.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent champ2= new Intent(pickchamp2.this,MainActivity.class);
            champ2.putExtra("myImageResource2", R.drawable.ac);
            champ2.putExtra("message2", "ac");
            startActivity(champ2);
        }
    });
}

}

and here is the second activity that i`d like to select the second image

package com.example.alex.matchups;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

public class pickchamp extends MainActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.AppTheme_Dialog);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pickchamp);
    getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, 
ViewGroup.LayoutParams.MATCH_PARENT);
    //--DECLARING IMAGEBUTTONS--//
    ImageButton ab2= (ImageButton)findViewById(R.id.ab2);
    ImageButton av2= (ImageButton)findViewById(R.id.av2);
    ImageButton ac2= (ImageButton)findViewById(R.id.ac2);


    //--ONCLICKLISTENERS--//
    ab2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent act2= new Intent(pickchamp.this,MainActivity.class);
            act2.putExtra("myImageResource", R.drawable.ab2);
            act2.putExtra("message", "ab");
            startActivity(act2);
        }
    });
    av2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent act2= new Intent(pickchamp.this,MainActivity.class);
            act2.putExtra("myImageResource", R.drawable.av2);
            act2.putExtra("message", "av2");
            startActivity(act2);
        }
    });
    ac2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent act2= new Intent(pickchamp.this,MainActivity.class);
            act2.putExtra("myImageResource", R.drawable.ac2);
            act2.putExtra("message", "ac2");
            startActivity(act2);
        }
    });
}}

Thanks very much indeed

redberry
  • 696
  • 7
  • 15

3 Answers3

0

It looks like every time your main activity is created you reset the images for all of your image views.

So when you launch an activity with an Intent you are creating a new object effectively, its not the same activity which already has an image set. When you're launching your main activity for the second time you aren't passing in an argument for one of the image views so it sets it to the default value you specify.

image.setImageResource(getIntent().getIntExtra("myImageResource",R.drawable.random)); image2.setImageResource(getIntent().getIntExtra("myImageResource2",R.drawable.random));

because you are only passing in myImageResource OR myImageResource2 when the activity is launched one or the other will be set to default, basically the one you haven't passed in.

An easy solution for this is to check if the argument is there and set the image view resource if it is.

if(getIntent().hasExtra("myImageResource2")) { image2.setImageResource(getIntent().getIntExtra("myImageResource2",R.drawable.random)); } if(getIntent().hasExtra("myImageResource")) { image.setImageResource(getIntent().getIntExtra("myImageResource",R.drawable.random)); }

  • Its not working, I dont understand the whole idea of this check honestly – redberry Jul 20 '17 at 14:52
  • change the two lines i've specified and wrap them in an if – Stevenkang90 Jul 20 '17 at 15:04
  • `if(getIntent().hasExtra("myImageResource")) { image.setImageResource(getIntent().getIntExtra("myImageResource",R.drawable.random)); }` – Stevenkang90 Jul 20 '17 at 15:05
  • what that does is only set the image if you have passed a value in the intent – Stevenkang90 Jul 20 '17 at 15:05
  • I see what you mean but that's not how it works. The application has 2 images set as default for both imageviews which is the random image and than by clicking on 2 different buttons it opens 2 different activities which should change the images for the 2 imageviews. What this if does is removing the default image and messing up pretty much everything. The first part of the explanation is really useful tho. I will try to implement startFromActivityResult I guess and if that doesnt work I will just maek them with a dialog in the activity and not using multiple activities – redberry Jul 20 '17 at 15:11
0

As Steven said, you're creating new activities every time, and that way u can't achieve what you want.

A better solution is to finish your pickchamp's activities and passing the image back to MainActivity and not create another MainAcitivity.

You can do that using startActivityForResult to send your data back to MainActivity as described here .

Code

Start your pickchamp and pickchamp2 using startActivityForResult:

Intent intent = new Intent(MainActivity.this, pickchamp.class);
startActivityForResult (intent, 1); //where 1 is the requestCode that you want to use

Intent intent = new Intent(MainActivity.this, pickchamp2.class);
startActivityForResult (intent, 2); //where 2 is the requestCode that you want to use

And add/override onActivityResult inside the MainActivity

 @Override  
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
       super.onActivityResult(requestCode, resultCode, data); 
       if(requestCode == 1) {  
         image.setImageResource(data.getIntExtra("myImageResource",R.drawable.random));  
       }
       else if(requestCode == 2) { 
image2.setImageResource(data.getIntExtra("myImageResource2",R.drawable.random));
           }
     }  

On the pickchamp's activities you have to replace the onClick to methods something like that (example with pickchamp2)

Intent champ2= new Intent(pickchamp2.this,MainActivity.class);
champ2.putExtra("myImageResource2", R.drawable.ab);
champ2.putExtra("message2", "ab");
setResult(Activity.RESULT_OK, champ2);
finish();
Ruann Reis
  • 159
  • 8
  • Could you give me a bit more of an explanation of how this whole thing would be imported into my code because I`m relatively new to android and I cant quite figure it out – redberry Jul 20 '17 at 14:57
0

What you need, can be done by using onActivityResult .First remove below code from main Activity.

//--SETTING RESOURCES FROM ANOTHER ACTIVITY--//
image.setImageResource(getIntent().getIntExtra("myImageResource",R.drawable.random));
image2.setImageResource(getIntent().getIntExtra("myImageResource2",R.drawable.random));

Now you have to overwrite the onActivityResult method of Your MainActivity.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode==100){
        if(resultCode==10){
            image.setImageResource(data.getIntExtra("myImageResource",R.drawable.random);
        }else if(resultCode==11){
            image2.setImageResource(getIntent().getIntExtra("myImageResource2",R.drawable.random));
        }
    }

Now every time you trigger an Intent from your main activity call startActivityForResult() which take two parameters Intent and request code.

Intent intent = new Intent(MainActivity.this, YouTargetClassName.class);
            startActivityForResult(intent,100);

Now in Your pickchamp Class replace this code

Intent act2= new Intent(pickchamp.this,MainActivity.class);
        act2.putExtra("myImageResource", R.drawable.ab2);
        act2.putExtra("message", "ab");
        startActivity(act2);

With below code

Intent act2= new Intent(pickchamp.this,MainActivity.class);
            act2.putExtra("myImageResource", R.drawable.ab2);
            act2.putExtra("message", "ab");
            setResult(10,act2);
            finish();

Lastly in pickchamp2 class replace

Intent champ2= new Intent(pickchamp2.this,MainActivity.class);
        champ2.putExtra("myImageResource2", R.drawable.av);
        champ2.putExtra("message2", "av");
        startActivity(champ2);

with

Intent champ2= new Intent(pickchamp2.this,MainActivity.class);
            champ2.putExtra("myImageResource2", R.drawable.av);
            champ2.putExtra("message2", "av");
            setResult(11,champ2);
            finish();

Run your code and confirm it works for you or not. Feel free to ask in case you need clarification :)

  • That's it! It works, thank you very much. You just need to correct on the else if statement its data.getintdata() and it works so well.! – redberry Jul 20 '17 at 23:39