title: navigating between simple activities
body: <p><strong>Hello</strong> </p>
  
  <p><img src="https://i.stack.imgur.com/cnBPO.png" alt="fig"></p>
  
  <p><em>I want to know how this concept is implemented in android</em></p>
  
  <ul>
  <li>Navigation in this application takes place like this::</li>
  </ul>
  
  <blockquote>
    <p>Activity1- -- to- -  -> Activity 2- -to - -.>Activity 3 - - to - ->
    Activity2</p>
  </blockquote>
  
  <p><strong>But on doing  this ::</strong></p>
  
  <p><em>I pass the data from</em> </p>
  
  <blockquote>
    <p>Activity1 - - > Activity 2- -> I do not pass the data from Activity2 -
    - > Activity3</p>
  </blockquote>
  
  <p><strong>Now</strong></p>
  
  <blockquote>
    <p>If I navigate back to Activity2- - from - - Activity3</p>
  </blockquote>
  
  <ul>
  <li>The application closes because Activity2 is expecting the data which
  is not present in Activity3</li>
  <li>How can I overcome this, how can I preserve the state(even data) of
  activity 2 before navigating back from Activity3</li>
  </ul>
  
  <p><strong>Cycle has to be ::</strong> </p>
  
  <blockquote>
    <p>Activity1- -- to- -  -> Activity 2- -to - -.>Activity 3 - - to - ->
    Activity2</p>
  </blockquote>
  
  <hr>
  
  <ul>
  <li>How to achieve this?</li>
  <li>What concepts do i need to look for</li>
  </ul>
  
  <p>I hope i am clear with my description</p>
  
  <hr>
  
  <h2>I have given a sample program to support my question</h2>
  
  <p>how to modify code to achieve this</p>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends Activity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity1);
  
          Button BTN=(Button) findViewById(R.id.activity3button3);
          BTN.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
          Intent INT=new Intent(MainActivity.this,Activity2.class);
                  INT.putExtra("hi", "HI");
                  startActivity(INT);     
              }
          }); 
      }
  
   }
  </code></pre>
  
  <p><strong>Activity2.java</strong></p>
  
  <pre><code>public class Activity2 extends Activity{
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity2);
  
  
  
          Button BTN=(Button) findViewById(R.id.activity3button3);
  
          BTN.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
  
                  Intent INT=new Intent(Activity2.this,Activity3.class);
                  startActivity(INT);
  
              }
          });
  
      }
  
  
  
  }
  </code></pre>
  
  <p><strong>Activity3.java</strong></p>
  
  <pre><code>public class Activity3 extends Activity{
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          // TODO Auto-generated method stub
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity3);
  
          Button BTN=(Button) findViewById(R.id.activity3button3);
  
          BTN.setOnClickListener(new OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  Intent INT=new Intent(Activity3.this,Activity2.class);
                  startActivity(INT);
              }
          });
  
  
  
      }
  
  
  
  }
  </code></pre>