有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java带抽屉布局,无论哪个屏幕处于活动状态,如何在按下后退键时关闭应用程序?

我正在使用抽屉布局来导航我的应用程序:

以该菜单为例:

enter image description here

我想用Home按钮启动HomeActivity等等。但是清除活动堆栈,也就是说:如果我按“人”,然后按“照片”,然后按“位置”,然后按“后退”按钮,应用程序应该会关闭。我尝试在启动活动时使用意图标志,但我总是得到相同的行为:人->;照片->;位置->;返回转到照片,而不是关闭应用程序

我怎样才能做到这一点


共 (2) 个答案

  1. # 1 楼答案

    正如你所说,“如果我按“人”,然后按“照片”,然后按“位置”,然后按“后退”按钮,应用程序应该关闭”

    为了实现这一点,你应该为家庭、人、地点和照片等用户片段。 当你们按下设备的“后退”按钮时,你们就可以清除碎片回到堆栈,完成活动

  2. # 2 楼答案

    我想这个代码可能会对你有所帮助。当你按下设备的“后退”按钮时,它将关闭应用程序

    在主要活动中。爪哇

    public void SelectItem(int possition) 
    {
    
                //Using Fragment here
        Fragment fragment = null;
        Bundle args = new Bundle();
        switch (possition) 
        {
    
               //Can use Fragment for Home,People,Location and Photos etc. and when you press back button of Device you can clear Fragment back stack and finish activity.
                 case 0:
            fragment=new FragmentNewsFeed();
                        break;
    
                 case 1:
            fragment = new FragmentMessages();
            break;
    
                default:
            break;
        }
    
        fragment.setArguments(args);
        FragmentManager frgManager = getFragmentManager();
        frgManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
    
        listView_drawer.setItemChecked(possition, true);
        setTitle(dataList.get(possition).getItemName());
        drawerLayout.closeDrawer(listView_drawer);
    
    }
    

    在FragmentNewsFeed类中

    public class FragmentNewsFeed extends Fragment 
    {
    
    
       public FragmentNewsFeed()
       {
    
       }
      
       @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) 
       {
          View view = inflater.inflate(R.layout.fragment_news_feeds, container,false);
          return view;
       }
           }
    

    而您可以在布局/活动主目录中找到内容框架。xml

    <android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <!  Framelayout to display Fragments  >
    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
    
    <!  Listview to display slider menu  >
    
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#ffff"/>
        </android.support.v4.widget.DrawerLayout>
    

    在右布局中。片段(新闻)(feed),

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linear_Parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#008B8B"
    android:orientation="vertical" >
    
    <LinearLayout
        android:id="@+id/linear_1"
        android:layout_width="80dp"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:layout_weight="0.30"
        android:background="#2F4F4F"
        android:orientation="vertical" >
    
        <ListView
            android:id="@+id/listView_OwnItems"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
        </ListView>
    </LinearLayout>
    </LinearLayout>