如何在现有活动之间切换?
我正在设计一个应用程序,它是一种文章阅读器和文章阅读器。经理和文章有不同语言版本。
因此,当用户阅读俄语的特定文章时,有一个按钮可以用英语显示同一篇文章,反之亦然。
为此,我开始新的活动。 我现在所拥有的是,如果用户多次按下同一篇文章的“翻译”按钮,将会出现大量重复的活动。
我需要实现的是,当用户首先将俄语文章“翻译”为英语,然后按英语文章中的“翻译”按钮时,应用程序将他返回到显示俄语文章的现有活动,并且不会启动新的活动。这是一个代码,它没有做我需要说明我的尝试的事情。
intent.putExtra("BookID", strBookID );
intent.putExtra("ChapterNum", mCurrChapterNum );
intent.putExtra("TextNum", mCurrTextNum );
try{
if (Central.LastBookID.equals(""))
{ //remember current article id
Central.LastBookID=strBookID;
Central.LastChapter=mCurrChapterNum;
Central.LastText=mCurrTextNum;
}
else
{
if (Central.LastChapter.equals(mCurrChapterNum) &&
Central.LastText.equals(mCurrTextNum) )
intent.addFlags(Intent. FLAG_ACTIVITY_REORDER_TO_FRONT);
}
startActivity(intent);
请给我一些建议,也许以不同的方式实施会更好? 从我读到的内容来看,Android 没有任何“活动 ID”,因此,如果我在堆栈中有 2 个同一类“A”的活动,但具有不同的参数,例如(A1 A2),我可以告诉系统将到前面的活动 A1 并完成它 (A2 A1)
I am designing an app which is a kind of articles reader & manager and articles available in different languages.
So when user reads a particular article in Russian there is a button to display the same article in English and vice versa.
To do that I start new activity.
What I have now is that if user presses "translate" button of the same article several times there will be heaps of duplicate activities.
What I need to achieve is that when user first "translates" Russian article to English, then presses "translate" button in an English article, the app returns him to existing activity displaying Russian Article and does not start a new Activity. Here is a code which does not do what I need to illustrate my attempts.
intent.putExtra("BookID", strBookID );
intent.putExtra("ChapterNum", mCurrChapterNum );
intent.putExtra("TextNum", mCurrTextNum );
try{
if (Central.LastBookID.equals(""))
{ //remember current article id
Central.LastBookID=strBookID;
Central.LastChapter=mCurrChapterNum;
Central.LastText=mCurrTextNum;
}
else
{
if (Central.LastChapter.equals(mCurrChapterNum) &&
Central.LastText.equals(mCurrTextNum) )
intent.addFlags(Intent. FLAG_ACTIVITY_REORDER_TO_FRONT);
}
startActivity(intent);
Please give me some advice, maybe it would be better to implement it differently?
From what I managed to read, Android does not have any "Activity ID"s so that if I have 2 activities of the same class "A" in stack but with different parameters like (A1 A2), I could tell the system to bring to front activity A1 and make it (A2 A1)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用上面的标志将完成前一个标志并开始新的标志。所以没有一堆活动,也没有额外的逻辑。
using the above flag instead will finish the prevoius one and starts the new one. so no heap of Activities and no extra logic.