简体   繁体   English

android-如何从应用程序注销

[英]android - How to Logout from the application

My Application have 5 activities(A1,A2,A3,A4,A5). 我的应用程序有5个活动(A1,A2,A3,A4,A5)。 Each activity have one text view and one button(B1,B2,B3,B4,B5). 每个活动都有一个文本视图和一个按钮(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. 假设如果您单击B1按钮,则转到A2活动,如果每个活动都具有一个菜单按钮(注销),则单击该按钮将退出应用程序。 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. 在注销按钮单击侦听器中,我使用finish()完成了当前活动。因为我们现在使用FLAG_ACTIVITY_CLEAR_TOP清除了堆栈,所以堆栈仅包含当前活动,这就是为什么我仅完成当前活动。 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 . 这里使用该语句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 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 谢谢

You have to set setResult(int resultCode) on the activity where you want to logout. 您必须在要注销的活动上设置setResult(int resultCode) Then on previous Activity you have to capture this result in onActivityResult(int requestCode, int resultCode,Intent data) . 然后,在先前的Activity上,您必须在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. 再次在这里捕获,您可以setResult关闭上一个相同的方法。 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);
}

Suppose you have four activities (A,B,C,D) and you want to develop Logout functionality in your app using Menu buttons. 假设您有四个活动(A,B,C,D),并且想要使用菜单按钮在应用程序中开发注销功能。

Step 1: First Declare a Variable in 步骤1:首先在中声明变量

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 步骤2:使用菜单onclick注销按钮

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. 它将重定向到DashBoard Activity,然后由于SearchHelper.Logout==1 ,它将再次重定向注销Activity。 Finally, like that you can logout from anywhere using the menu buttons. 最后,您可以使用菜单按钮从任何地方注销。 without problem of onBackPressed() . 没有onBackPressed()问题

Or use Single instance in manifest For Activity and handle each and every Activity onBackPressed() . 或在清单清单中为活动使用Single实例,并处理每个活动onBackPressed()

You can also do logout by starting the LoginActivity. 您也可以通过启动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;
    }
}

You have to close your activities (all of them) every time with finish() when you open the next one. 每次打开下一个活动时,都必须使用finish()来关闭所有活动。 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() . 注意:startActivity()之前或之后调用finish() startActivity()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM