android - How to Logout from the application
<p>My Application have 5 activities(A1,A2,A3,A4,A5). Each activity have one text view and one button(B1,B2,B3,B4,B5). If you click on that button then goes to next activity. suppose if you click on the B1 button then it goes to A2 activity and one more thing each activity have one menu button(Logout) if you click that button then it will exit from the application. But it is not working. 
Here i am using the following code for every activity calling. </p>

<p><strong>For clear the stack</strong></p>

<pre><code> Intent intent = new Intent(act1.this,act2.class);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    startActivity(intent); 
</code></pre>

<p>In Logout button click listener, i finished current activity using the <code>finish()</code>.because we are clear the stack using <code>FLAG_ACTIVITY_CLEAR_TOP</code> now stack contains current activity only thats why i just finish the current activity. But if you click on the Logout button it just finish current actvity only. It not exit from the application. Here stack is cleared or not using that statement <code>FLAG_ACTIVITY_CLEAR_TOP</code>. Following is my code can anybody help me. </p>

<p><strong>Actvity one</strong></p>

<pre><code> public class logout extends Activity

    {

          TextView tv;

        Button next;

        public static final int logout_menu = Menu.FIRST+1;

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            TextView tv = (TextView) findViewById(R.id.text);
            tv.setText("activity1");

            Button next = (Button) findViewById(R.id.button);
            next.setOnClickListener(nextListener);

        }

        private OnClickListener nextListener = new OnClickListener()
        {
            public void onClick(View v)
            {           
                try
                {                   
                    Intent intent = new Intent(logout.this,act2.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    startActivity(intent);              
                }
                catch(Exception ex2)
                {
                    System.out.println("Not able to launch Registration Screen"+ex2.toString());
                }
            }
        };

        public boolean onCreateOptionsMenu(Menu menu){
            // TODO Auto-generated method stub
            boolean result = super.onCreateOptionsMenu(menu);
            menu.add(0, logout_menu, 0,  "Logout");   
            return result;
        }

        public boolean onOptionsItemSelected(MenuItem item){        
            // TODO Auto-generated method stub
            switch (item.getItemId()) {
                case logout_menu:finish();

                    break;  
            }
            return super.onOptionsItemSelected(item);
        }                  
    }
</code></pre>

<p><strong>Actvity2</strong></p>

<pre><code> public class act2 extends Activity

 {

        TextView tv;

    Button next;
    public static final int logout_menu = Menu.FIRST+1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TextView tv = (TextView) findViewById(R.id.text);
        tv.setText("activity2");

        Button next = (Button) findViewById(R.id.button);
        next.setOnClickListener(nextListener);

    }

    private OnClickListener nextListener = new OnClickListener()
    {
        public void onClick(View v)
        {           
            try
            {                   
                Intent intent = new Intent(act2.this,act3.class);
                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);              
            }
            catch(Exception ex2)
            {
                System.out.println("Not able to launch Registration Screen"+ex2.toString());
            }
        }
    };

    public boolean onCreateOptionsMenu(Menu menu){
        // TODO Auto-generated method stub
        boolean result = super.onCreateOptionsMenu(menu);
        menu.add(0, logout_menu, 0,  "Logout");   
        return result;
    }

    public boolean onOptionsItemSelected(MenuItem item){        
        // TODO Auto-generated method stub
        switch (item.getItemId()) {
            case logout_menu:finish();

                break;  
        }
        return super.onOptionsItemSelected(item);
    }                   
}
</code></pre>

<p>thanks</p>
