如何保持活动的先前状态 [英] How to maintain the previous state of an activity

查看:110
本文介绍了如何保持活动的先前状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个活动(activity1和activiy2),每个活动都有一个按钮.在activity1中,我有一个很少选择的微调框.假设我从此微调器中选择了选项2,并且我单击了activity1中的按钮,则activity2开始.当我单击后退"按钮时,activity1恢复,并且相同的选项2可见(就像我需要的一样).现在的问题是,如果我的activity2已启动并且我单击其中的一个按钮,则activity1已启动.但是,与其恢复先前活动1的状态,不如以刚创建并更改先前选择的方式开始.即使再次启动活动,我如何也能获得与后退"按钮相同的功能(不是返回上一个活动的功能,我的意思是自动恢复任何活动的先前状态).我只是需要知道如果再次访问某个活动,该如何保持该活动的先前状态.

I am having two activities (activity1 and activiy2) and each activity is having one button each. In activity1 i having a spinner with few options. Suppose i am selecting option 2 from this spinner and i am clicking the button in activity1, then activity2 starts. When i click back button activity1 is resumed and the same option 2 is visible (like i need). Now the problem is that if my activity2 is started and i am clicking a button in it, activity1 is started. But instead of resuming the previous state of activity1 it starts in a way that it has just created and the previous selection is changed. How can i get the same facility like back button (not the facility of going back to previous activity, i mean automatically resuming the previous state of any activity) even when i start the activity again. Simply i need to know how to maintain the previous state of an activity if it is again visited.

使用此代码,单击按钮时,我从一个活动转到另一个活动:

It is with this code I go from one activity to another when button is clicked:

Intent intent=new Intent();
intent.setClassName(getApplicationContext(),"com.myapp.activityname");

startActivity(intent);

请帮助我.我是android的初学者,所以如果有人给出答案,请解释一下.谢谢

Kindly help me.I am a beginner in android, so if any one is giving the answer please explain it a bit. Thanks in adavnce

推荐答案

我认为找到了答案.让我用简单的话说一下我做过的事情,

Think I found the answer. Let me tell what I have done in simple words,

假设我有两个活动Activity1和Activity2,并且我正在从活动1导航到活动2(我在活动2中完成了一些工作),然后通过单击活动1中的按钮再次回到活动1.现在,在这个阶段,我想回到活动2,并希望在我最后一次离开活动2时看到我的活动2处于相同的状态.

Suppose i am having two activities activity1 and activity2 and i am navigating from activity1 to activity2(i have done some works in activity2) and again back to activity 1 by clicking on a button in activity1. Now at this stage I wanted to go back to activity2 and i want to see my activity2 in the same condition when I last left activity2.

对于上述情况,我所做的是在清单中进行了如下更改:

For the above scenario what i have done is that in the manifest i made some changes like this:

<activity android:name=".activity2"
          android:alwaysRetainTaskState="true"
          android:launchMode="singleInstance">
</activity>

在activity1上的按钮单击事件上,我是这样完成的:

And in the activity1 on the button click event i have done like this:

Intent intent=new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.setClassName(this,"com.mainscreen.activity2");
startActivity(intent);

在activity2的按钮单击事件中,我这样做是这样的:

And in activity2 on button click event i have done like this:

Intent intent=new Intent();
intent.setClassName(this,"com.mainscreen.activity1");
startActivity(intent);

现在将发生的事情是,无论我们对活动2所做的更改如何,都不会丢失,并且我们可以在与先前离开的状态相同的状态下查看活动2.

Now what will happen is that whatever the changes we have made in the activity2 will not be lost, and we can view activity2 in the same state as we left previously.

我相信这就是答案,这对我来说很好.

I believe this is the answer and this works fine for me.

这篇关于如何保持活动的先前状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆