2

I need to setImageResource in Activity A's ImageView after clicking a button in Activity B. I'm trying to do this creating a public static ImageView.

Here's the code of Activity A:

public class ActivityA extends Activity {

public static ImageView image;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activityA);

    image = (ImageView) findViewById(R.id.image);

}

Here's the code of Activity B:

button.setOnClickListener(new OnClickListener() {


        @Override
        public void onClick(View button) {

            if(editText.getText().toString().equalsIgnoreCase("myText")) {
                ActivityA.image.setImageResource(R.drawable.other_image);

            Intent act2= new Intent(ActivityB.this,ActivityA.class);
                startActivity(act2);    
            }




        }); 
}

I can't understand why my App crashes after button is clicked (it works if I remove "activityA.image.setImageResource(R.id.other_image)" ) so the problem must be here. Should I write something else in ActivityA.class? Thank you for every possible solution and sorry for my bad english.

Alexis C.
  • 91,686
  • 21
  • 171
  • 177
Gio31290
  • 89
  • 1
  • 3
  • 8

5 Answers5

1

you can do it like this:

public class ActivityA extends Activity {

public static ImageView image;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activityA);

image = (ImageView) findViewById(R.id.image);
image.setImageResource(getIntent().getIntExtra("myImageResource",R.drawable.default_image);
}

*Note: I used R.drawable.default_image in case that "myImageResource" not found

button.setOnClickListener(new OnClickListener() {


    @Override
    public void onClick(View button) {


        Intent act2= new Intent(ActivityB.this,ActivityA.class);
            act2.putExtra("myImageResource", R.drawable.other_image);
            startActivity(act2);    
        }




    }); 
 }
Saif Hamed
  • 1,084
  • 1
  • 12
  • 17
0

Don't do that. A static reference to a view will keep the entire Activity alive. Once an activity is stopped, it is killable by the system.

Depending on what exactly you're trying to do, you can pass the resource id to the new instance of ActivityA as an intent extra, or use startActivityForResult when starting ActivityB, then implement onActivityResult in ActivityA.

FunkTheMonk
  • 10,908
  • 1
  • 31
  • 37
0

When you run into an app crash, you should always post the logCat for it. My guess, though, is that you're hitting a null pointer exception, because some part of ActivityA.image is null.

In any event, this isn't the best way to change Activity A from Activity B. Remember that when Activity B is in the foreground, Activity A isn't visible. The change to Activity A's UI can only occur when Activity A comes to the foreground. Therefore, what you're really doing is changing the state of Activity A, not its UI. To keep track of Activity A's state, use persistent storage (SharedPreferences) or some other app-wide storage. When Activity A comes back to the foreground in onResume(), modify the UI if necessary to reflect the current state.

Displaying an app's instantaneous state rather than its initial state is why most UI updates should be in onResume() instead of onCreate(). Similarly, always save the Activity's state in onPause().

Finally, in Android you should avoid using public global variables to communicate data between components (Activities, Services, ContentProviders, and BroadcastReceivers). Instead, use Intents or persistent storage.

Joe Malin
  • 8,621
  • 1
  • 23
  • 18
0

you are trying to change the image of activity A in activity B .activity B does not inflate the view of your activity A.it is not presnt in Activity B.then definitely there will be an exception thrown and hence the app crash.instead pass the extra value using put extra from activity B to A. check in activity A with getIntent.getExtras(). and if it has some value then change the image of Activity A

khubaib
  • 535
  • 4
  • 12
0

LogCat would help understand your issue, but I would suggest that you remove the static value and try PutExtra on you Intent, coz As far as i know you cannot directly send or manipulate data between two separate activities like you did. so

private ImageView image; 

on activity A

and in B

act2.putExtra("myImageResource", R.drawable.other_image);

dont forget to check in activity A if the bundle exists

Intent intent = new Intent();
    intent = getIntent();
    Bundle bundle = intent.getExtras();

    if (bundle != null) {...}
Jony-Y
  • 1,579
  • 1
  • 13
  • 30