title: Why does Android Intent back to original Activity sometimes crash the app?
body: <p>This bug has been tricky to find for me because when I initially start the Main Activity and press the help button, which begins an <code>Intent</code> to start the Help Activity, there is no problem.  Likewise, in the Help Activity pressing the button I made to go back, which starts an Intent for the main activity works fine.  </p>
  
  <p>However, after the user plays the game and gets a game over screen and returns from the high scores screen, then <em>sometimes</em> but not always pressing the help screen will cause the app to crash. Why is that? I imagine it has something to do with the activity life cycle, but seeing as I'm new to android development I must not be understanding it fully.  </p>
  
  <p><strong>Help class</strong></p>
  
  <pre><code>public class Help extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_help);
  
          final ImageButton back = (ImageButton)findViewById(R.id.back);
          back.setOnClickListener(new OnClickListener(){
              public void onClick(View view){
                  finish();
              }
          });
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.help, menu);
          return true;
      }
  }
  </code></pre>
  
  <p><strong>MainActivity</strong></p>
  
  <pre><code>public class MainActivity extends Activity implements SensorEventListener{
  
      private static final String TAG = "uVuMain";
      public SensorManager smanager;
      public Boolean playing = false, paused = false, firstPlay = true;
      public TextView score;
      public MediaPlayer mediaPlayer;
      long startTime, elapsedTime, lastUpdate;
      float moveIncrement, middle, moveRightLimit, moveLeftLimit, 
      difficulty;
      Timer timer;
      Boolean test = true;
      ScoreDB scoreDB;
      LinkedList&lt;Integer&gt; ll = new LinkedList&lt;Integer&gt;();
  
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          setContentView(R.layout.activity_main);
          score = (TextView)findViewById(R.id.score);
          score.setTextColor(Color.WHITE);
  
          //get the sensor and music ready
          smanager = (SensorManager)this.getSystemService(SENSOR_SERVICE);
          setVolumeControlStream(AudioManager.STREAM_MUSIC);
          mediaPlayer = new MediaPlayer();
          mediaPlayer.start();
          try {
              AssetManager assetManager = getAssets();
              AssetFileDescriptor descriptor = assetManager.openFd("uVuLoop.mp3");
              mediaPlayer.setDataSource(descriptor.getFileDescriptor(),
                      descriptor.getStartOffset(), descriptor.getLength());
              mediaPlayer.prepare();
              mediaPlayer.setLooping(true);
          } catch (IOException e) {
              Log.d(TAG, "could not load music: "+e.getMessage());
              mediaPlayer = null;
          }
  
          //mute option 
              final ImageButton ib = (ImageButton)findViewById(R.id.mute);
              ib.setOnClickListener(new OnClickListener(){
                  public void onClick(View view){
                      if (mediaPlayer.isPlaying()){
                      mediaPlayer.pause();
                      ib.setImageResource(R.drawable.mute);
                      }
                      else {
                          mediaPlayer.start();
                          ib.setImageResource(R.drawable.unmute);
                      }
                  }
              });
  
          //pause option
          //should not work if game hasn't started or if it's gameover
              final ImageButton pause = (ImageButton)findViewById(R.id.pause);
              pause.setOnClickListener(new OnClickListener(){
                  public void onClick(View view){
                      if (playing &amp; !paused){
                          playing = false;
                          paused = true;
                          pause.setImageResource(R.drawable.play);
  
                          //stop the background
                          //added for testing
                          RelativeLayout rl = (RelativeLayout) findViewById(R.id.main);
                          AnimationDrawable frameAnimation = (AnimationDrawable) rl.getBackground();
                          frameAnimation.stop();
  
                          //stop the character animation
                          ImageView c = (ImageView)findViewById(R.id.mainChar);
                          c.setImageResource(R.drawable.normal_0);
  
                          if (timer != null){
                              timer.pause();
                          }
                      }
                      else if (!playing &amp; paused) {
                          playing = true;
                          paused = false;
                          pause.setImageResource(R.drawable.pause);
  
                          //start the background
                          //added for testing
                          RelativeLayout rl = (RelativeLayout) findViewById(R.id.main);
                          AnimationDrawable frameAnimation = (AnimationDrawable) rl.getBackground();
                          frameAnimation.start();
  
                          if (timer != null){
                              timer.resume();
                          }
                      }
                  }
              });
              pause.setEnabled(false);
  
          //access to scoreboard for adding scores
          CursorFactory cf = null;
          scoreDB = new ScoreDB(this, "scoreDB", cf);
  
          final ImageButton help = (ImageButton)findViewById(R.id.help);
          help.setOnClickListener(new OnClickListener(){
              public void onClick(View view){
                  Intent h = new Intent(MainActivity.this, Help.class);
                  startActivity(h);
              }
          });
  
          //start the game
          final ImageButton start = (ImageButton)findViewById(R.id.start);
          start.setOnClickListener(new OnClickListener(){
              public void onClick(View view) {                
                  CountDown timer = new CountDown(3000, 500);
                  timer.start();
                  pause.setEnabled(true);
                  start.setEnabled(false);
                  help.setEnabled(false);
              }
          });
  
  
      }
  
      //give the player a moment to get ready
      public class CountDown extends CountDownTimer{
          public CountDown(long s, long i){
              super(s, i);
          }
          ImageButton countDown = (ImageButton)findViewById(R.id.start);
  
          @Override
          public void onFinish() {
              countDown.setImageResource(R.drawable.go);
              playing = true;
              startTime = System.currentTimeMillis();
              score.setVisibility(View.VISIBLE);
  
              //Start background animation
              RelativeLayout rl = (RelativeLayout) findViewById(R.id.main);
              rl.setBackgroundResource(R.drawable.background);
              AnimationDrawable frameAnimation = (AnimationDrawable) rl.getBackground();
              frameAnimation.start();
  
              //Start Character animation
              ImageView mainChar = (ImageView)findViewById(R.id.mainChar);
              mainChar.setImageResource(R.drawable.maincharacter);
  
              //Make sure gameover screen is invisible
              ImageView gameOver = (ImageView)findViewById(R.id.gameover);
              gameOver.setVisibility(View.INVISIBLE);
  
              if (firstPlay){
                  //Calculate screen movement 
                  int[]posXY= new int[2];
                  mainChar.getLocationInWindow(posXY);
                  middle = posXY[0];
                  //changed from .005 to .01 for faster start 
                  moveIncrement = (float)(middle*.01); 
                  moveRightLimit = (float) (middle+0.5*middle);
                  moveLeftLimit= (float)(middle-0.5*middle);
                  firstPlay = false;
              }
              mainChar.setX(middle);
  
              //Start the clock
              timer = new Timer(startTime);
          }
  
          @Override
          public void onTick(long millisUntilFinished) {
              performTick(millisUntilFinished, countDown);
          }
      }
  
      public void performTick(long millisUntilFinished, ImageButton count){
          int remaining = Math.round(millisUntilFinished * 0.001f);
          if (remaining == 3){
              count.setImageResource(R.drawable.num_3);
          }
          else if (remaining == 2){
              count.setImageResource(R.drawable.num_2);
          }
          else if (remaining == 1){
              count.setImageResource(R.drawable.num_1);
          }
      }
  
      public void onSensorChanged(SensorEvent event){
          if (event.sensor.getType()==Sensor.TYPE_ACCELEROMETER &amp;&amp; playing){
              getAccelerometer(event);
          }
      }
  
      public void getAccelerometer(SensorEvent event){
          //In intervals of 10 seconds increase the difficulty 
          elapsedTime = timer.getElapsedTime()/1000;
          difficulty = ((elapsedTime/10)+1);
  
          //only need the y axis and want avg for smoother controls
          float[]values = event.values;
          int orientation = Math.round(values[1]);
          orientation = smoothing(orientation);
  
          score.setText("Walked "+elapsedTime+" feet.\nLevel: "+difficulty);
  
          //Move across screen in a timed interval
          //Changed from 15 to 25
          if (System.currentTimeMillis()-lastUpdate&gt;25){
              lastUpdate = System.currentTimeMillis();
              move(orientation, difficulty);      
          }
      }
  
      public void move(int oriented, float difficulty){
          //Get character's current position
          int[]posXY= new int[2];
          ImageView mainChar = (ImageView)findViewById(R.id.mainChar);
          mainChar.getLocationInWindow(posXY);
          int currentPosition = posXY[0];
          if (currentPosition &gt; middle){
  
          }
  
          float delta = (float) (moveIncrement*difficulty*2);
          if (oriented&gt;=0){
              if (oriented == 0){
                  oriented = 1;
              }
              delta = delta*((float)oriented/10);
  //          Check right boundary
              if ((currentPosition+delta)&gt;moveRightLimit){
                  mainChar.setX(moveRightLimit);
                  mainChar.setImageResource(R.drawable.fall_right);
                  gameOver();
              }
              else{
  //          Log.d(TAG, "Oriented value: "+oriented+"\nDelta value: "+delta);
                  mainChar.setX(currentPosition+delta);
                  tilt(mainChar, currentPosition+delta);
              }
  
          }
          else {
              delta = -delta*((float)oriented/10);
  //          Check left boundary
              if ((currentPosition-delta)&lt;moveLeftLimit){
                  mainChar.setX(moveLeftLimit);
                  mainChar.setImageResource(R.drawable.fall_left);
                  gameOver();
              }
              else {
  //          Log.d(TAG, "Oriented value: "+oriented+"\nDelta value: "+delta);
                  mainChar.setX(currentPosition-delta);
                  tilt(mainChar, currentPosition-delta);
              }
          }
      }
  
      public void tilt(ImageView mainChar, float position){
  
              if (position&lt; (middle-middle*.10)){
                  mainChar.setImageResource(R.drawable.maincharacterleft); 
              }
              else if(position&gt;(middle+middle*.10)){
                  mainChar.setImageResource(R.drawable.maincharacterright);
              }
              else {
                  mainChar.setImageResource(R.drawable.maincharacter);
              }
      }
  
      public void gameOver(){
          //stop the background
          RelativeLayout rl = (RelativeLayout) findViewById(R.id.main);
          AnimationDrawable frameAnimation = (AnimationDrawable) rl.getBackground();
          frameAnimation.stop();
  
          //fade in the gameover screen
          Animation animation = new AlphaAnimation(0, 1);
          animation.setInterpolator(new AccelerateInterpolator());
          animation.setDuration(1000);
          ImageView gameOver = (ImageView)findViewById(R.id.gameover);
          gameOver.setVisibility(View.VISIBLE);
          gameOver.startAnimation(animation);
  
          //Reset buttons
          ImageButton startbtn = (ImageButton)findViewById(R.id.start);
          startbtn.setImageResource(R.drawable.play_again);
          playing = false;
          ImageButton hlp = (ImageButton)findViewById(R.id.help);
          hlp.setEnabled(true);
          startbtn.setEnabled(true);
  
          //update scoreboard
          scoreDB.addScore((int) elapsedTime);
  
          animation.setAnimationListener(new AnimationListener(){
  
              @Override
              public void onAnimationEnd(Animation arg0) {
                  // TODO Auto-generated method stub
                  Intent i = new Intent(MainActivity.this, ScoreBoard.class);
                  startActivity(i);
              }
  
              @Override
              public void onAnimationRepeat(Animation arg0) {
                  // TODO Auto-generated method stub
  
              }
  
              @Override
              public void onAnimationStart(Animation arg0) {
                  // TODO Auto-generated method stub
  
              }
  
          });
      }
  
      //take average of last 4 values to smooth controls
      private int smoothing(int value){
          ll.add(value);
  
          if (ll.size()&gt;4){
              ll.removeFirst();
          }
  
          int sum = 0;
          for (Integer i: ll){
              sum+=i;
          }
  
          int avg = Math.round(sum/((float)ll.size()));
          return avg;
      }
  
      @SuppressWarnings("static-access")
      protected void onResume(){
          super.onResume();
  
          if (mediaPlayer != null) {
              mediaPlayer.start();
          }
  
          smanager.registerListener(this, 
          smanager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
          smanager.SENSOR_DELAY_NORMAL);
      }
  
      protected void onPause() {
          super.onPause();
          smanager.unregisterListener(this);
  
          if (mediaPlayer != null) {
              mediaPlayer.pause();
              if (isFinishing()) {
                  mediaPlayer.stop();
                  mediaPlayer.release();
              }
          }
      }
  
      @Override
      public boolean onCreateOptionsMenu(Menu menu) {
          // Inflate the menu; this adds items to the action bar if it is present.
          getMenuInflater().inflate(R.menu.main, menu);
          return true;
      }
  
      @Override
      public void onAccuracyChanged(Sensor sensor, int accuracy) {
          // TODO Auto-generated method stub
  
      }
  }
  </code></pre>
  
  <p>I've been stumped on this and reading the Android docs on <code>Intent</code>s didn't seem to help.  On a possibly related note, if the music is muted, when I go back from the help screen it becomes un-muted. If this is related to my issue please let me know as well.  </p>
  
  <p><strong>Logcat</strong></p>
  
  <pre><code>10-01 14:44:55.149: E/AndroidRuntime(27704): FATAL EXCEPTION: main
  10-01 14:44:55.149: E/AndroidRuntime(27704): Process: com.uVu.caffeineRun, PID: 27704
  10-01 14:44:55.149: E/AndroidRuntime(27704): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uVu.caffeineRun/com.uVu.caffeineRun.Help}: android.view.InflateException: Binary XML file line #1: Error inflating class &lt;unknown&gt;
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.os.Handler.dispatchMessage(Handler.java:102)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.os.Looper.loop(Looper.java:136)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.app.ActivityThread.main(ActivityThread.java:5001)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at java.lang.reflect.Method.invokeNative(Native Method)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at java.lang.reflect.Method.invoke(Method.java:515)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at dalvik.system.NativeStart.main(Native Method)
  10-01 14:44:55.149: E/AndroidRuntime(27704): Caused by: android.view.InflateException: Binary XML file line #1: Error inflating class &lt;unknown&gt;
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.view.LayoutInflater.createView(LayoutInflater.java:620)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:669)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:694)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.app.Activity.setContentView(Activity.java:1929)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at com.uVu.caffeineRun.Help.onCreate(Help.java:20)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.app.Activity.performCreate(Activity.java:5231)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    ... 11 more
  10-01 14:44:55.149: E/AndroidRuntime(27704): Caused by: java.lang.reflect.InvocationTargetException
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at java.lang.reflect.Constructor.constructNative(Native Method)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.view.LayoutInflater.createView(LayoutInflater.java:594)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    ... 23 more
  10-01 14:44:55.149: E/AndroidRuntime(27704): Caused by: java.lang.OutOfMemoryError
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:594)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:429)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.content.res.Resources.loadDrawable(Resources.java:2110)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.view.View.&lt;init&gt;(View.java:3554)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.view.View.&lt;init&gt;(View.java:3484)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.view.ViewGroup.&lt;init&gt;(ViewGroup.java:464)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    at android.widget.RelativeLayout.&lt;init&gt;(RelativeLayout.java:236)
  10-01 14:44:55.149: E/AndroidRuntime(27704):    ... 26 more
  </code></pre>