活动导航问题 [英] Activity Navigation Problem

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

问题描述

这可能是一个简单的问题,我有一个Android应用程序,其中我有几个页面。现在假设在开始时我正在进行第1页的工作。然后我切换到第2页。然后到第3页。同时我再次想要使用第1页。所以我使用startActivity(intent)返回该页面。但是会发生什么呢?页面1再次创建,即一个新实例。



建议我如何在不创建新实例的情况下使用先前堆叠的第1页实例。



另请注意,pg-1是应用程序的主页,因此在转到第2页时我无法使用finish()方法。

This might be a simple question, I have one android app in which I have several pages. Now suppose in starting I am working on page-1 . then I switch over to page-2. then to page-3. meanwhile I again want to use page-1. so I use startActivity(intent) to go back to that page. But what happens is page-1 is created once again i.e. a new instance.

Suggest me how to use that previously stacked instance of page-1 without making new instance.

Also note that pg-1 is home page of app so I cannot use finish() method while going to page-2.

推荐答案



问题是由于launchmode引起的。如果您没有更改任何内容,则使用标准模式。因此,将始终在标准模式下创建实例。一种解决方案是使用标签格式,因此您可以在标签和活动之间切换。另一个解决方案是将变量值保存在Sharedpreferences中并重新启动新活动(通过编写'Activityname.this.finish()'来完成以前的活动,或者你可以编写android:launchmode =singleTask来所有活动一次只能有一项活动)

或:

http://stackoverflow.com/questions/7202612/how-to-go-back-to-3rd-previous-activity-in-android [ ^ ]





你可以在每个活动中使用finish(),或者你可以开始这个标志为FLAG_ACTIVITY_CLEAR_TOP的第一个活动,它将清除你的第一个活动的所有顶级活动,它将把你的第一个活动放在首位。使用这样:



In tent intent = new Intent(this,yourFirstActivity.class);

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(intent);
Hi,
The problem is caused due to launchmode. You are using standard mode if you've not changed anything. Therefore, an instance will always be created in standard mode. One solution is using tab format, thus you can switch between tabs and activities. The other solution is keeping the values of variables in Sharedpreferences and relaunch in the new activity(by the way you could finish the previous activities by writing 'Activityname.this.finish()' or you could write android:launchmode="singleTask" to all your activity to have only one activity at a time)
or:
http://stackoverflow.com/questions/7202612/how-to-go-back-to-3rd-previous-activity-in-android[^]

:
you can use finish() in every activity as you proceed or you can start your first activity with this flag FLAG_ACTIVITY_CLEAR_TOP and it will clear all the top activity of your first activity and it will bring your first activity on top.Use like this:

Intent intent = new Intent(this, yourFirstActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);


这篇关于活动导航问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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