Addev
Addev

Reputation: 32233

With a DrawerLayout, how can I close the app on pressing Back no matter which screen is active?

I'm using a DrawerLayout for the navigation of my app:

Taking this menu for the example:

enter image description here

I'd like the Home button to launch the HomeActivity and so on. But clearing the activity stack, that is: If I press People, then Photos, then Locations and then the back button, the app should close. I'm trying intent flags when launching the activities but I always get the same behaviour: People->Photos->Location->Back goes to Photos instead closing the app

How can I achieve that?

Upvotes: 0

Views: 298

Answers (2)

Achiever
Achiever

Reputation: 1636

I think this code may help you. When you press Back button of a device it will close the application.

In MainActivity.java,

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);

}

In FragmentNewsFeed class,

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;
   }
       }

Whereas you can find content frame in a layout/activity_main.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>

In R.layout.fragment_news_feeds,

<?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>

Upvotes: 1

Herry
Herry

Reputation: 7087

As per you said that "If I press People, then Photos, then Locations and then the back button, the app should close".

For achieve that you should user 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.

Upvotes: 1

Related Questions