title: Need code for exit from app in android
body: <p>Hello i am new to android.I am implementing some application and it have some activities.
  Suppose if i launch the app for first time,it's entering in to A then going to B after that C,D,E..... (Here A,B,C,D,E are activities).If i press back button at E then it is going D--> C--> B-->  A like this.</p>
  
  <p>Now i want to implement code to exit/quit from the app when i am at D.
            I wrote following code but this code is working for closing current activity and going to prev activity.means going C.</p>
  
  <blockquote>
    <p><strong>finish();</strong></p>
  </blockquote>
  
  <p>Then i tried with following code and it is working fine and closing current application successfully and going to device home screen.But if i want open the application again then it is starting form D instead of A.</p>
  
  <blockquote>
  <pre><code>  intent = new Intent(Intent.ACTION_MAIN);
      intent.addCategory(Intent.CATEGORY_HOME);
      intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
      startActivity(intent);
  </code></pre>
  </blockquote>
  
  <p>copied from <a href="https://stackoverflow.com/questions/3226495/android-exit-application-code">here</a></p>
  
  <p>Please help me to solve my problem.</p>