0

Say I have activity 1 and activity 2, and image A and image B.

Currently, on activity 2 I have the background image set to image A, is there a way to change that background image to image B from inside of activity 1?

Edit:

I tried

    LinearLayout layoutBg = (LinearLayout) findViewById(R.id.activity2);
    layoutBg.setBackgroundResource(0);

from inside for activity 1, but I get a nullpointerexception:

 Caused by: java.lang.NullPointerException
        at com.imperostudio.meddit.StepTwo.onResume(StepTwo.java:141)
        at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1198)
        at android.app.Activity.performResume(Activity.java:5530)
        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3048)
            at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3087)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1345)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5579)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)
  • Look at the log, there is a Null Pointer Exception at line 41 of StepTwo class (onResume method). You are trying to get LinearLayout in activity 2 inside activity 1 – Phuc Tran Jul 28 '15 at 04:55
  • [Android - set layout background programmatically](http://stackoverflow.com/questions/12678949/android-set-layout-background-programmatically) – pRaNaY Jul 28 '15 at 05:40

3 Answers3

1

You can use SharedPreferences to save primitive data: Like strings And CHECKING/MATCHING .

How to use SharedPreferences in Android to store, fetch and edit values

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

You can use SharedPreferences to store the desired background and upon creating Activity2 you can read the background again from SharedPreferences and set it properly. Do it like this

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
SharedPreferences.Editor editor = prefs.edit();
editor.putString(KEY, VALUE);
editor.commit();

And read it like this:

SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);
try {
    String value = sharedPrefs.getString(KEY, defaultValue);
} catch (Exception e) {
    e.printStackTrace();
}
2hamed
  • 8,719
  • 13
  • 69
  • 112
0

I guess above answers wont work as user2809361 wants to change the image in realtime.The best option would be to use broadcast recievers. use this code in activity 2

private BroadcastReceiver mMyReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            // change your image from here you can get the image link from intent
        }
    };
registerReceiver(mMyReceiver, new IntentFilter(...));

and then from activity 1 use this code

Intent intent = new Intent(this,MyReceiver.class);
intent.putExtra("image", "imagelink");
this.sendBroadcast(intent);

this will trigger your broadcast receiver in Activity 2 and it will call the method on receive then you can do your task there.

Hope this helps!!

Huzefa Gadi
  • 1,129
  • 1
  • 7
  • 11