简体   繁体   English

使用导航抽屉和后退按钮在活动之间正确导航?

[英]Proper navigation between activities using navigation drawer and back button?

I have the following situation: 我有以下情况:

One Main ListActivity . 一个主要的ListActivity The list consists of POJO objects, each item is a different POJO. 该列表由POJO对象组成,每个项目都是一个不同的POJO。

Each POJO contains different groups. 每个POJO包含不同的组。 For each group I made different Activity . 对于每个小组,我进行了不同的Activity

Lets say POJO1 contains groups A , B and C , when pressed, ActivityA will be opened and the navigation between the rest activities ActivityB and ActivityC is done using NavigationDrawer . 比方说POJO1包含组A,BC,按下时,ActivityA将被打开,其余活动ActivityBActivityC之间的导航使用进行NavigationDrawer

All group activities implement a custom class MyNavigationDrawerClass which is the core of the Navigation Drawer in my app. 所有小组活动都实现一个自定义类MyNavigationDrawerClass ,这是我应用程序中Navigation Drawer的核心。

Each ActivityA , B , C , etc, has a SettingsActivity which is strictly individual and depends on from where it has been started. 每个ActivityABC等都有一个SettingsActivity ,它严格独立,并取决于从何处开始。

Now the question is: How to navigate from any A , B , C , etc, activity to the Main ListActivity every time the BackButton is pressed? 现在的问题是:每次按下BackButton时,如何从任何ABC等活动导航到Main ListActivity?

I will provide an example: 我将提供一个示例:

  1. App is started - Main List Activity is showed; 应用已启动-显示主列表活动
  2. Item is pressed and ActivityA with NavigationDrawer is started; 按下项目并启动带有NavigationDrawer ActivityA
  3. The NavigationDrawer is opened and ActivityC is opened; 打开NavigationDrawer并打开ActivityC
  4. When BackButton is pressed, the user is navigated back to ActivityA , but my desire is to navigate the user to the Main List Activity 当按下BackButton ,用户被导航回到ActivityA ,但是我的愿望是将用户导航到Main List Activity

What I tried: 我试过的

  1. To put android:noHistory="true" for every ActivityA , ActivityB and ActivityC in the AndroidManifest . AndroidManifest中的每个ActivityAActivityBActivityC放置android:noHistory="true" That did not work because when I open the SettingsActivity for example from ActivityA and return, the user in navigated to the Main List Activity , and not to the ActivityA - it's confusing and misleading. 那是行不通的,因为例如当我从ActivityA打开SettingsActivity并返回时,用户导航到Main List Activity ,而不是ActivityA-这令人困惑和误导。
  2. To override onBackPressed in MyNavigationDrawerClass and start the Main List Activity using Intent . 要覆盖onBackPressed中的onBackPressed并使用Intent启动主列表活动 That did not work either because there are still previous activities in the stack and when the BackButton is pressed from Main List Activity , the user is navigated to them. 这也不起作用,因为堆栈中仍然存在先前的活动,并且当从Main List Activity中按下BackButton时,用户被导航到了这些活动。

I hope I explained the situation clearly. 我希望我能清楚地说明情况。

Any help will be appreciated. 任何帮助将不胜感激。

You can override the public void onBackPressed(){...} for redirect the user where you want like this : 您可以覆盖public void onBackPressed(){...}以便将用户重定向到所需的位置,如下所示:

public void onBackPressed()
{
    Intent intent = new Intent(this.getApplicationContext(), MainListActivity.class);
    startActivity(intent);    
} 

look at this link can help you 看这个链接可以帮到你

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

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