title: Set imageview after orientation change
body: <p>I have the following code, which is working perectly fine till I rotate my phone. Then I have to click again to load an image, I understand that when we rotate the activity restarts and we have some methods to store the state and restore it, but in my case as you can see that the img file is in a string as it is generated at random.
  So how can I make use of onConfigurationChanged (which seems easy to understand) to restore the previous image before rotation?</p>
  
  <pre><code>public class HomeScreen extends Activity {
  
      protected ImageView imgView;
      protected String str;
  
      @Override
      protected void onCreate(final Bundle savedInstanceState) {
  
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_home_screen);
          final Random rnd = new Random();
          ActionBar actionBar = getActionBar();
          actionBar.hide();
  
          imgView = (ImageView) findViewById(R.id.imgRandom);
  
          if (savedInstanceState != null) {
              str = savedInstanceState.getString("param");
              imgView.setImageDrawable
                      (
                              getResources().getDrawable(getResourceID(str, "drawable", getApplicationContext()))
                      );
  
  
  
  
              imgView.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View view) {
  
  
  
                     // final ImageView img = (ImageView) findViewById(R.id.imgRandom);
                      // I have 3 images named img_0 to img_2, so...
                      str = "img" + rnd.nextInt(9);
                      imgView.setImageDrawable
                              (
                                      getResources().getDrawable(getResourceID(str, "drawable", getApplicationContext())));
  
  
                  }
  
  
              });
  
  
          }
      }
  
      protected void onSaveInstanceState(Bundle savedInstanceStat) {
          super.onSaveInstanceState(savedInstanceStat);
          savedInstanceStat.putString("param", str);
      }
  </code></pre>
  
  <p>When I rotate it is crashing and also on load OR onClick no images are loading..</p>
  
  <pre><code>&gt; 07-07 21:11:32.950: I/InputReader(468): Device reconfigured: id=1,
  &gt; name='Genymotion Virtual Input', size 1080x1920, orientation 0, mode
  &gt; 1, display id 0 07-07 21:11:32.950: I/ActivityManager(468): Config
  &gt; changes=480 {1.0 310mcc260mnc en_US ?layoutDir sw360dp w360dp h567dp
  &gt; 480dpi nrml port finger qwerty/v/v dpad/v s.14} 07-07 21:11:33.081:
  &gt; W/ResourceType(1861): Too many attribute references, stopped at:
  &gt; 0x01010034 07-07 21:11:33.081: W/ResourceType(1861): Too many
  &gt; attribute references, stopped at: 0x01010034 07-07 21:11:33.082:
  &gt; D/AndroidRuntime(1861): Shutting down VM 07-07 21:11:33.083:
  &gt; E/AndroidRuntime(1861): FATAL EXCEPTION: main 07-07 21:11:33.083:
  &gt; E/AndroidRuntime(1861): Process:
  &gt; app.motivation.techiequickie.ypb.motivation, PID: 1861 07-07
  &gt; 21:11:33.083: E/AndroidRuntime(1861): java.lang.RuntimeException:
  &gt; Unable to start activity
  &gt; ComponentInfo{app.motivation.techiequickie.ypb.motivation/app.motivation.techiequickie.ypb.motivation.HomeScreen}:
  &gt; java.lang.NullPointerException: name is null 07-07 21:11:33.083:
  &gt; E/AndroidRuntime(1861):   at
  &gt; android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
  &gt; 07-07 21:11:33.083: E/AndroidRuntime(1861):   at
  &gt; android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
  &gt; 07-07 21:11:33.083: E/AndroidRuntime(1861):   at
  &gt; android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3947)
  &gt; 07-07 21:11:33.083: E/AndroidRuntime(1861):   at
  &gt; android.app.ActivityThread.access$900(ActivityThread.java:151) 07-07
  &gt; 21:11:33.083: E/AndroidRuntime(1861):     at
  &gt; android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
  &gt; 07-07 21:11:33.083: E/AndroidRuntime(1861):   at
  &gt; android.os.Handler.dispatchMessage(Handler.java:102) 07-07
  &gt; 21:11:33.083: E/AndroidRuntime(1861):     at
  &gt; android.os.Looper.loop(Looper.java:135) 07-07 21:11:33.083:
  &gt; E/AndroidRuntime(1861):   at
  &gt; android.app.ActivityThread.main(ActivityThread.java:5254) 07-07
  &gt; 21:11:33.083: E/AndroidRuntime(1861):     at
  &gt; java.lang.reflect.Method.invoke(Native Method) 07-07 21:11:33.083:
  &gt; E/AndroidRuntime(1861):   at
  &gt; java.lang.reflect.Method.invoke(Method.java:372) 07-07 21:11:33.083:
  &gt; E/AndroidRuntime(1861):   at
  &gt; com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
  &gt; 07-07 21:11:33.083: E/AndroidRuntime(1861):   at
  &gt; com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 07-07
  &gt; 21:11:33.083: E/AndroidRuntime(1861): Caused by:
  &gt; java.lang.NullPointerException: name is null 07-07 21:11:33.083:
  &gt; E/AndroidRuntime(1861):   at
  &gt; android.content.res.Resources.getIdentifier(Resources.java:2034) 07-07
  &gt; 21:11:33.083: E/AndroidRuntime(1861):     at
  &gt; app.motivation.techiequickie.ypb.motivation.HomeScreen.getResourceID(HomeScreen.java:148)
  &gt; 07-07 21:11:33.083: E/AndroidRuntime(1861):   at
  &gt; app.motivation.techiequickie.ypb.motivation.HomeScreen.onCreate(HomeScreen.java:35)
  &gt; 07-07 21:11:33.083: E/AndroidRuntime(1861):   at
  &gt; android.app.Activity.performCreate(Activity.java:5990) 07-07
  &gt; 21:11:33.083: E/AndroidRuntime(1861):     at
  &gt; android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
  &gt; 07-07 21:11:33.083: E/AndroidRuntime(1861):   at
  &gt; android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
  &gt; 07-07 21:11:33.083: E/AndroidRuntime(1861):   ... 11 more 07-07
  &gt; 21:11:33.084: W/ActivityManager(468):   Force finishing activity 1
  &gt; app.motivation.techiequickie.ypb.motivation/.HomeScreen
  </code></pre>