简体   繁体   English

如何返回上一个活动?

[英]How to go back to the previous activity?

I am developing an application with 5 interfaces 我正在开发具有5个接口的应用程序

I have an activity where the user has to enter a password, after entering it the user moves to the next activity, but when the user clicks on back button and he's returned to the password activity, the entered password is missing... how can I maintain the entered password? 我有一个活动,用户必须输入密码,输入密码后,用户将移至下一个活动,但是当用户单击“后退”按钮并返回到密码活动时,输入的密码丢失了……怎么办我保持输入的密码吗?

Code: 码:

back = (Button) findViewById(R.id.back);
back.setOnClickListener(new View.OnClickListener() {    

        public void onClick(View v1) {
            Intent backIntent = new Intent();
            backIntent.setClass(getApplicationContext(), LockAppActivity.class);
            backIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
            startActivity(backIntent);
            finish();
        }
    });

Suppose the app starts with activities A --> B --> C --> D --> E 假设应用程序以活动A-> B-> C-> D-> E开始

When user moves to B from C, and at activity B there is a "deactivate" button and when the user presses it all the user settings are cleared and the application will exit, but when I press the button, it brings me to the layout C... how can I change this? 当用户从C移到B时,在活动B处有一个“停用”按钮,当用户按下它时,所有用户设置均被清除,应用程序将退出,但是当我按下该按钮时,它将带我进入布局C ...我该如何更改?

    deactivate = (Button) findViewById(R.id.deactivate);
    deactivate.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v){ 
            final String STORAGE = "UniqueID_IMSI";
            SharedPreferences unique = getSharedPreferences(STORAGE, 0);
            SharedPreferences.Editor editor1 = unique.edit();
            editor1.putString("identifier", "");
            editor1.putString("simIMSI", "");
            editor1.commit();

            final String LOGIN_PASSWORD = "Login_Password";     
            SharedPreferences loginPassword = getSharedPreferences(LOGIN_PASSWORD, 0);
            SharedPreferences.Editor editor2 = loginPassword.edit();
            editor2.putString("loginPassword", "");
            editor2.commit();

            final String LOCK_APP_PASSWORD = "LockAppPassword";
            SharedPreferences userPassword = getSharedPreferences(LOCK_APP_PASSWORD, 0);
            SharedPreferences.Editor editor3 = userPassword.edit();
            editor3.putString("password", "");
            editor3.commit();

            final String PHONE_NUMBER = "Phone_Number";         
            SharedPreferences phone = getSharedPreferences(PHONE_NUMBER, 0);
            SharedPreferences.Editor editor4 = phone.edit();
            editor4.putString("phoneNumber", "");
            editor4.commit();

            finish();
        }
    });
}

how can i maintain the entered password 我如何保持输入的密码

Save the password entered by the user in a SharedPreference and in onResume() function of the Activity recalled the saved password from the SharedPrefrence and show it to user. 将用户输入的密码保存在SharedPreference中,并在Activity的onResume()函数中,从SharedPrefrence调出保存的密码并将其显示给用户。 You may like to spend sometime trying to understand the Activity lifecycle. 您可能想花一些时间来了解活动的生命周期。

application will exit 申请将退出

Have a look at this link . 看一下这个链接 @Romain Guy, is a Google guy. @Romain Guy,是Google家伙。 You may wish to rethink about your app design. 您可能希望重新考虑您的应用程序设计。

my favorite solution to the closing-app problem are: 对于关闭应用程序问题,我最喜欢的解决方案是:

  • consider using "startActivityForResult" for activities that you wish to close when a specific event has occurred . 考虑将“ startActivityForResult”用于您希望在发生特定事件时关闭的活动。
  • you can also use some intents flags , but i consider them quite messy and try to use as little of them as possible 您还可以使用一些Intent标志,但是我认为它们很乱,并尝试使用尽可能少的Intent标志
  • use fragments instead of activities . 用片段代替活动。 you can choose exactly which fragment to show and when. 您可以精确选择显示哪个片段以及何时显示。

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

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