简体   繁体   English

Android开发-使用活动或片段进行导航

[英]Android development - Navigating using Activities or fragments

Im new to android development and have a question about application navigation. 我是android开发的新手,对应用程序导航有疑问。

At the moment I have a number of items in an OptionsMenu that drops down in the top right of the app. 目前,OptionsMenu中有许多项目,它们位于应用程序右上角。 All of my activities has this same OptionsMenu. 我所有的活动都具有相同的OptionsMenu。 Each item in that menu has an OnClick callback that launches a new activity to correspond with that part of the application. 该菜单中的每个项目都有一个OnClick回调,该回调启动一个新活动以与应用程序的该部分相对应。 This is the way im currently navigating round 'pages' in my application. 这是我当前在应用程序中浏览“页面”的方式。 Code for the onMenuebutton click callback is as follows: onMenuebutton单击回调的代码如下:

Intent i = new Intent(c, Activity2.class); 意图i =新的意图(c,Activity2.class); c.startActivity(i); c.startActivity(i);

The first time an OptionsMenu item is selected its fine, a new activity is created and the app moves to it. 第一次选择OptionsMenu菜单项时,会创建一个新活动并将应用程序移至该活动。 However, when I select that item again, a new activity is created, even if one for that option is already running. 但是,当我再次选择该项目时,即使该选项的某个活动已经在运行,也会创建一个新的活动。 I can get in the situation where I have a stack of a number of the same activities. 我遇到一堆很多相同的活动的情况。

My question is, is there any way to resume an already running activities rather than creating new ones? 我的问题是,有什么办法可以恢复已经在运行的活动,而不是创建新的活动? Perhaps I am doing this the wrong way and instead should be swapping in and out fragments on a single activity? 也许我这样做的方式错误,而是应该在单个活动中切入和切出片段?

Thanks 谢谢

You dont need write extra code.Supose if Activity A is already running,dont want to lunch another same Activity.So you just declare android manifest file like this. 您不需要编写额外的代码。假设如果Activity A已经在运行,则不要午餐另一个同一个Activity。因此,您只需要这样声明android清单文件即可。

<activity android:name=".com.app.ActivityA" android:launchMode="singleTask" android:configChanges="orientation|keyboardHidden" android:screenOrientation="portrait"></activity>

Yes of-course the fragment is good options instead of launch new activity. 当然,片段是不错的选择,而不是启动新的活动。

Then better to go with Fragments. 然后最好使用Fragments。

or 要么

else you can finish your previous activity whenever start the new activity (If you are not having back button) 否则,无论何时开始新活动,您都可以完成上一个活动(如果没有后退按钮)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM