旋转设备进行两项活动时程序流程出现问题

发布于 2024-09-14 09:29:41 字数 1102 浏览 1 评论 0原文

我的第一个活动是在其 onCreate 中创建第二个活动:

if (userName == null || password == null) {
    if (!getUserNameAndPassword() ) {
        // User is launching this for the first time
        Intent explicitIntent = new  Intent(CreateSessionAlert.this,CreateUserNameAndPassword.class);<br>
        startActivityForResult(explicitIntent,GET_USER_NAME_AND_PASSWORD);
    }
}

第二个活动具有用户名和密码的 EditText 视图以及提交和取消按钮。当按下任一按钮时,用户名和密码中的文本将发送回活动一(通过 onActivityResult),该活动连接到服务器以查看用户名是否可用。连接到服务器时,showDialog 用于显示 ProgressDialog。当服务器响应时,ProgressDialog 将被关闭并显示 AlertDialog。只要设备不旋转,此功能就可以正常工作。

问题是:如果用户在第二个 Activity 中旋转设备并按下提交,第一个 Activity 会重新调用其 onCreate,最终会再次启动第二个 Activity,因为用户名和密码仍然为空。

有人有任何建议来解决这个问题吗?

是否有一种方法可用于将数据从 Activity-2 传递到 Activity-1 的 onCreate?在这种情况下我不想使用数据库,因为这看起来有点矫枉过正。

我考虑过包含活动 2 的服务器通信和对话框,但是活动 1 和活动 2 中会有重复的服务器代码(活动 1 也连接到服务器进行其他操作)。我使用这种方法遇到的另一个问题是用户可以选择取消创建密码,在这种情况下我希望应用程序完成。如果我从活动 2 中调用完成,则会出现活动 1,但我不希望显示该活动,除非已创建用户名和密码。

我突然想到——如果我可以用对话框替换活动 2,事情可能会变得更简单。对话框视图需要有 2 个 EditText 和 2 个按钮。我可以创建这样的东西并使用 showDialog 方法吗?还有其他想法吗?

My first activity is creating a second activity within its onCreate:

if (userName == null || password == null) {
    if (!getUserNameAndPassword() ) {
        // User is launching this for the first time
        Intent explicitIntent = new  Intent(CreateSessionAlert.this,CreateUserNameAndPassword.class);<br>
        startActivityForResult(explicitIntent,GET_USER_NAME_AND_PASSWORD);
    }
}

The second activity has EditText views for the username and password along with submit and cancel buttons. When either button is pushed, the text from username and password is sent back to activity one (via onActivityResult), which connects to a server to see if the username is available. While connecting to the server showDialog is used to show a ProgressDialog. When the server responds, the ProgressDialog is dismissed and an AlertDialog is shown. This works fine as long as the device is not rotated.

Here's the problem: if the user rotates the device from within the second activity and pushes submit, the first activity re-invokes its onCreate, which ends up launching the second activity again since the username and password are still null.

Does anyone have any suggestions to get around this?

Is there a method that could be used to pass data from activity-2 to activity-1's onCreate? I wouldn't want to use a database in this case, as that seems like overkill.

I've thought about containing the server communication and dialogs to activity 2, but then I'd have duplicate server code in activity 1 and activity 2 (activity 1 connects to the server for other things too). Another issue I'd have with this approach is that the user has an option to cancel creating the password, in which case I want the app to finish. If I called finish from activity 2, activity 1 would appear and I don't want that to show unless a username and password have been created.

It just occurred to me - if I could replace activity 2 with a dialog it might make things simpler. The dialog view needs to have 2 EditTexts and 2 buttons. Can I create something like this and use the showDialog method? Any other ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫儰鉨ミ守葔 2024-09-21 09:29:41

即可

<activity android:name="..."
      android:label="@string/appName"
      android:configChanges="keyboardHidden|orientation" />

您只需将:添加到清单中的活动中 。这将防止 Activity 在屏幕旋转时被破坏。

请注意,您仍然必须在 onPause 方法中保存活动的状态。只需测试如果在用户登录或在登录期间接听电话时按下主页按钮会发生什么。这应该会从内存中删除您的 Activity,并且可能会出现更多问题。

You can just add:

<activity android:name="..."
      android:label="@string/appName"
      android:configChanges="keyboardHidden|orientation" />

to the activity in your manifest. This will prevent the activity from being destroyed on the screen rotation.

Beware that you still have to save the state of your activities in the onPause method. Just test what happens if you press the home button while the user logs in or recieve a call during logging in. This should remove your Activity from the memory and some more problems may occur.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文