如何在单击后退按钮并返回到同一个 Activity 时保持 Activity 的状态

发布于 2024-11-30 00:43:28 字数 821 浏览 1 评论 0原文

我有一个与不同活动之间的导航相关的问题, onPause 和 onRestore 可能会解决该问题,但我不知道如何解决。

问题是 我的应用程序中有以下屏幕

  1. HomeActivity - 这是欢迎屏幕
  2. CategoryActivity - 这里有三个类别,例如动物、植物和水果。
  3. QuestionsActivity - 这里每个问题都可以根据类别的选择来使用。每个类别有 50 个问题。该应用程序有多项选择类型的问题。

因此,应用程序的流程是,当用户选择一个类别时,我从 xml 中填充该类别的 50 个问题的数组列表,然后将这些问题一一显示在 QuestionActivity 屏幕上。 在这里,每次用户单击类别时,我都会对数组列表进行洗牌 并显示计数。即您完成了 1 个问题,您完成了 2 个问题等。

这是关于我的应用程序和现在的问题:

假设用户在 QuestionActivty 中,他回答了 5 个问题,然后单击移动设备的后退按钮,因此他被重定向到 CategoryActivity 屏幕。现在,从这里开始,如果他再次单击同一类别,他将被重定向到 QuestionActivity 屏幕,但他的问题从第一个问题开始:(。理想情况下,应该从第六个问题开始,因为他已经问了 5 个问题。

有人可以帮忙吗我来解决这个问题吗?如果我坚持计数,那么它对我来说不起作用,因为每次我都在洗牌数组列表(所以回答的问题可能会重复。) 理想的解决方案应该是让android在单击后退按钮时管理QuestionActivity的状态,并在再次选择相同类别时恢复相同的状态

这一定是一个常见问题,所以如果您已经解决了它,请帮帮我。

预先非常感谢, 伊山

I have a question related to navigation between different activities and onPause and onRestore might solve the problem but how that I don't know.

The problem is
I have following screens in my application

  1. HomeActivity - This is the welcome screen
  2. CategoryActivity - Here there are three categories e.g. Animal, Plants and Fruits.
  3. QuestionsActivity - Here each questions are available according to selection of Category. In each cateogry there are 50 questions.The application has multiple choice type of questions.

so the flow of application is when a user comes and selects a category, I populate an arraylist of 50 questions from my xml for that category, then display those questions one by one on the QuestionActivity screen.
Here, every time a user clicks on the category, I shuffle the arraylist
and show the count. i.e you finished 1 question, you finished 2 question etcs.

So this is something about my application and now problem:

Suppose a user is in QuestionActivty and he answered 5 questions and then click on back button of mobile so he is redirected to the CategoryActivity screen. Now from here, if he again clicks on the same Category, he is redirected to the QuestionActivity screen but his questions starts from 1st questions :(. Ideally, it should start with 6th question as he already aswered 5 questions.

Can some one please help me to solve this problem? If I persist the count, then it won't work for me as every time I am shuffling the arraylist (so the answered questions might repeat.)
What the ideal solution should be is to let android manage the state of QuestionActivity at the time of click of back button and resume the same state at the time of again selecting the same Category

This must be a common problem so if you already solved it, please help me out.

Thanks a lot in advance,
Ishan

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

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

发布评论

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

评论(2

夏有森光若流苏 2024-12-07 00:43:28

为什么不使用 StartActivityForResult()。将 int x = 5 发送回 CategoryActivity,然后当您恢复 QuestionActivity 时,您可以将 x 连同它一起发送回 Bundle extras 中。那么 QuestionActivity 就知道从第 6 (x+1) 个问题开始。

如果问题是随机排序的,您将会遇到问题,因为第六个问题可能与前五个问题相同。因此,不要只发送一个 int x 回来,而是发送一个已回答的问题编号的整数数组。

Why not use StartActivityForResult(). Send back an int x = 5 back to CategoryActivity and then when you resume QuestionActivity you can send x back with it in the Bundle extras. SO then QuestionActivity knows to start form the 6th (x+1) question.

If the questions are randomly ordered you will have problems, since the 6th question might be the same as the first five. So instead of sending just an int x back, send an Integer Array of the question numbers that have already been answered.

左岸枫 2024-12-07 00:43:28

我建议考虑将这些数据存储在 SQLite DB 或 SharedPreferences 文件中。通过这种方式,您可以记录用户停下来的位置,并根据这些存储机制之一内的值提出下一个问题。

http://developer.android.com/guide/topics/data/data -存储.html

I'd suggest looking into storing this data in either a SQLite DB or in a SharedPreferences file. This way you can record where your user has left off and present the next questions based on values within one of these storage mechanisms.

http://developer.android.com/guide/topics/data/data-storage.html

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