简体   繁体   English

android上的向后导航行为

[英]back navigation behavior on android

I have some activities in my app: A - Login Activity B - Main app Activity C , D ... - Inner Activities I want to implement a behaviour as follows: 我的应用程序中有一些活动: A-登录活动B-主应用程序活动CD ...-内部活动我要实现以下行为:

  1. when a user select the app icon OR select the app from the task list I want the back stack to be cleared and show Activity A 当用户选择应用程序图标或从任务列表中选择应用程序时,我希望清除后堆栈并显示活动A
  2. when a user passed A pressing the back on activity B will quit the application 当用户通过A时,按下活动B的后退将退出该应用程序
  3. when a user in an inner app pressing back will function "normally". 当内部应用程序中的用户按下时,将“正常”运行。

for example: activity stack A -> B -> C -> D pressing the back button the first time (top activity is D ) will make pop the D of the stack top and it will be: A -> B -> C clicking again (top activity is C ) will move us to A -> B and clicking again will quit the app (as described in 2) 例如:活动堆栈A- > B- > C- > D第一次按下返回按钮(顶部活动是D ),将弹出堆栈顶部D ,它是: A- > B- > C单击再次(顶部活动为C )会将我们移至A- > B并再次单击将退出该应用程序(如2中所述)

I implemented (2) by adding android:noHistory="true" to A 's properties in AndroidManifest.xml and tried to implement (1) by adding android:launchMode=singleTop to A 's properties in AndroidManifest.xml but when I do that (3) is breaked and clicking back when D is visible moves me directly to A . 我实现(2)通过添加android:noHistory="true" ,以A“中的属性AndroidManifest.xml ,并试图通过向工具(1) android:launchMode=singleTopA”中的属性AndroidManifest.xml但是当我做(3)被破坏,当D可见时单击以将我直接移到A。

how can I implement them all? 我该如何实施它们?

Thanks! 谢谢!

Detect Back Key presses: 检测后退按键按下:

 @Override
  public boolean onKeyDown(int keyCode, KeyEvent event) {
      if (keyCode == KeyEvent.KEYCODE_BACK) {
          //Do what you want, forinstance:
          finish(); //This will close the current Activity and will go back to the previous one
          }

      return super.onKeyDown(keyCode, event);
  }

To close D you can use finish(); 要关闭D,您可以使用finish(); like in in the top example. 就像上面的例子一样 This will close D and will bring you back to C . 这将关闭D并使您回到C。 Same with C and in B you normally shouldn close the App. C相同,在B中 ,通常不应该关闭该应用程序。 But if so, you could call somtehing like System.exit(0); 但如果是这样,您可以像System.exit(0);一样调用somtehing System.exit(0); to close the App. 关闭应用程序。

To define, which Activity is the parent Activity, go into the Manifest.xml and put things like this: This is for your 1. thing: 要定义哪个Activity是父Activity,请进入Manifest.xml并输入以下内容:这是针对您的1.事情:

      <activity  
       android:name=".ActivityD" 
       android:parentActivityName="com.example.ActivityA" >
       <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.ActivityA" />
       </activity>

You can override onbackpressed method and used intent 您可以覆盖onbackpressed方法和使用的意图

> Intent intent = new Intent(Intent.ACTION_MAIN);
> intent.addCategory(Intent.CATEGORY_HOME);
> intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
> startActivity(intent);

On activity b 活动b

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

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