2

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.

For clear the stack

 Intent intent = new Intent(act1.this,act2.class);

    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    startActivity(intent); 

In Logout button click listener, i finished current activity using the finish().because we are clear the stack using FLAG_ACTIVITY_CLEAR_TOP 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 FLAG_ACTIVITY_CLEAR_TOP. Following is my code can anybody help me.

Actvity one

 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);
        }                  
    }

Actvity2

 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);
    }                   
}

thanks

Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
naresh
  • 10,332
  • 25
  • 81
  • 124

4 Answers4

5

You have to set setResult(int resultCode) on the activity where you want to logout. Then on previous Activity you have to capture this result in onActivityResult(int requestCode, int resultCode,Intent data). Here you can Finish your Activity. Again capturing here you can setResult to close previous one and same approach. Ex.:

You set result on logout menu press as:

finish();                     //To finish your current acivity
setResult(R.id.common_menu_logout);

Then on previous activity:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(resultCode) {
        case R.id.common_menu_logout:           
            setResult(R.id.common_menu_logout);
            closeActivity();            // to close this activity
            break;  
    }
    super.onActivityResult(requestCode, resultCode, data);
}
sfmirtalebi
  • 370
  • 7
  • 16
Balaji Khadake
  • 3,449
  • 4
  • 24
  • 38
1

Suppose you have four activities (A,B,C,D) and you want to develop Logout functionality in your app using Menu buttons.

Step 1: First Declare a Variable in

SearchHelper.logout=0;//in SearchHelper Class
//OnCreate of Activity--DashBoard
if(SearchHelper.logout==1)
{
    Intent loginscreen=new Intent(this,LoginActivity.class);
    loginscreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    Toast.makeText(DashBoardActivity.this, "WELCOME TO LOGINSCREEN", Toast.LENGTH_SHORT).show(); 
    startActivity(loginscreen);
    this.finish();
    SearchHelper.logout=0;
}

Step 2: onclick on logout button using menu

Intent homescreen=new Intent(this,DashBoardActivity.class);
SearchHelper.logout=1;
homescreen.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(homescreen);
this.finish();        

It will redirect to DashBoard Activity then due to SearchHelper.Logout==1 it will again redirect logout Activity. Finally, like that you can logout from anywhere using the menu buttons. without problem of onBackPressed().

Or use Single instance in manifest For Activity and handle each and every Activity onBackPressed().

Jimbo
  • 25,790
  • 15
  • 86
  • 131
ranjan
  • 136
  • 1
  • 12
0

You can also do logout by starting the LoginActivity. Example is shown below and is for when you select the Log Out button in the menu.

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();
    switch (id) {
        case R.id.action_logout:
            LoginActivity.username.setText("");
            LoginActivity.password.setText("");
            Intent logout = new Intent(getApplicationContext(), LoginActivity.class);
            startActivity(logout);
            return true;
    }
}
Kekis2014
  • 145
  • 2
  • 16
-1

You have to close your activities (all of them) every time with finish() when you open the next one. Now, when user hits logout, there is only one of your application activity active and when this is closed, your application "exits".

Note: It doesen't matter whether you call finish() before or after startActivity().

Simley Jo
  • 33
  • 8
Indrek Kõue
  • 6,449
  • 8
  • 37
  • 69
  • I used `finish()` before every `startActivity(intent)`. Let me know if it works. – Indrek Kõue Jul 26 '11 at 08:10
  • I think this isn't a good way of doing things because this will break the back button functionality wont it? Its much better to use `onActivityResult()` and check the result code if logout code used then again `finish()` etc etc...this way the logout request bubbles up through each `Activity` from the origin logged out `Activity` all the way back. Just as Belaji Khadake has suggested. – wired00 Aug 09 '12 at 01:03