我有两组 Activity ,假设每组有 3 个 Activity ,(A1,B1,C1 || A2,B2,C2) 我从 A1 开始我的应用程序然后 -> B1 -> C1 我想从 C1 跳到-> A2 和 A2 如果我按回它应该存在应用程序而不是让我回到 C1,然后从 A2 我导航到 -> B2 -> C2。
所以基本上我想更改起始 Activity,就像我在一个 App 中有两个 App,当我切换到第二个 App 时,我必须清除 Activity Stack。那可能吗?有什么想法吗?
最佳答案
在我看来,您已经回答了自己的问题。你写道:
So it is basically I want to change the starting Activity, it is like I have two Apps in one App and when I flip to the second App I have to clear the Activity Stack.
我会这样做:
创建一个 DispatcherActivity,它是应用程序启动时启动的 Activity 。此 Activity 是您任务的根 Activity,负责启动 A1 或 A2,具体取决于...而不是调用 finish() 自身(即:它将被 A1 或 A2 覆盖,但仍然位于 Activity 堆栈的根部)。
在 A1 中,捕获“后退”键并告诉 DispatcherActivity 像这样退出:
@Override
public void onBackPressed() {
Intent intent = new Intent(this, DispatcherActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addExtra("exit", "true");
startActivity(intent);
}
这将清除任务堆栈直至根 Activity (DispatcherActivity),然后再次启动 DispatcherActivity 以达到此目的。
在 C1 中,要启动 A2,请执行以下操作:
Intent intent = new Intent(this, DispatcherActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addExtra("A2", "true");
startActivity(intent);
这将清除任务堆栈直至根 Activity (DispatcherActivity),然后再次启动 DispatcherActivity 以达到此目的。
在 DispatcherActivity 中,在 onCreate() 中,您需要根据 Intent 中的 extras 来确定要做什么,如下所示:
Intent intent = getIntent();
if (intent.hasExtra("exit")) {
// User wants to exit
finish();
} else if (intent.hasExtra("A2")) {
// User wants to launch A2
Intent a2Intent = new Intent(this, A2.class);
startActivity(a2Intent);
} else {
// Default behaviour is to launch A1
Intent a1Intent = new Intent(this, A1.class);
startActivity(a1Intent);
}
在 A2 中,使用与 A1 中相同的 onBackPressed() 重写来捕获“后退”键并告诉 DispatcherActivity 退出.
注意:我只是输入了这段代码,所以我还没有编译它,它可能并不完美。您的里程可能会有所不同 ;-)
关于java - Activity 堆栈问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11430993/
我想为Heroku构建一个Rails3应用程序。他们使用Postgres作为他们的数据库,所以我通过MacPorts安装了postgres9.0。现在我需要一个postgresgem并且共识是出于性能原因你想要pggem。但是我对我得到的错误感到非常困惑当我尝试在rvm下通过geminstall安装pg时。我已经非常明确地指定了所有postgres目录的位置可以找到但仍然无法完成安装:$envARCHFLAGS='-archx86_64'geminstallpg--\--with-pg-config=/opt/local/var/db/postgresql90/defaultdb/po
尝试通过RVM将RubyGems升级到版本1.8.10并出现此错误:$rvmrubygemslatestRemovingoldRubygemsfiles...Installingrubygems-1.8.10forruby-1.9.2-p180...ERROR:Errorrunning'GEM_PATH="/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/ruby-1.9.2-p180@global:/Users/foo/.rvm/gems/ruby-1.9.2-p180:/Users/foo/.rvm/gems/rub
我的最终目标是安装当前版本的RubyonRails。我在OSXMountainLion上运行。到目前为止,这是我的过程:已安装的RVM$\curl-Lhttps://get.rvm.io|bash-sstable检查已知(我假设已批准)安装$rvmlistknown我看到当前的稳定版本可用[ruby-]2.0.0[-p247]输入命令安装$rvminstall2.0.0-p247注意:我也试过这些安装命令$rvminstallruby-2.0.0-p247$rvminstallruby=2.0.0-p247我很快就无处可去了。结果:$rvminstall2.0.0-p247Search
由于fast-stemmer的问题,我很难安装我想要的任何rubygem。我把我得到的错误放在下面。Buildingnativeextensions.Thiscouldtakeawhile...ERROR:Errorinstallingfast-stemmer:ERROR:Failedtobuildgemnativeextension./System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/bin/rubyextconf.rbcreatingMakefilemake"DESTDIR="cleanmake"DESTDIR=
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
当我尝试安装Ruby时遇到此错误。我试过查看this和this但无济于事➜~brewinstallrubyWarning:YouareusingOSX10.12.Wedonotprovidesupportforthispre-releaseversion.Youmayencounterbuildfailuresorotherbreakages.Pleasecreatepull-requestsinsteadoffilingissues.==>Installingdependenciesforruby:readline,libyaml,makedepend==>Installingrub
我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www
我意识到这可能是一个非常基本的问题,但我现在已经花了几天时间回过头来解决这个问题,但出于某种原因,Google就是没有帮助我。(我认为部分问题在于我是一个初学者,我不知道该问什么......)我也看过O'Reilly的RubyCookbook和RailsAPI,但我仍然停留在这个问题上.我找到了一些关于多态关系的信息,但它似乎不是我需要的(尽管如果我错了请告诉我)。我正在尝试调整MichaelHartl'stutorial创建一个包含用户、文章和评论的博客应用程序(不使用脚手架)。我希望评论既属于用户又属于文章。我的主要问题是:我不知道如何将当前文章的ID放入评论Controller。
我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我
什么是ruby的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht