1

I have implemented a test sample of android drawer using activities instead of frames like in this SO answer.

I have 3 Activities that extends from DrawerActivity.
MainActivity that is the first activity when application is launched, ActivityA and ActivityB which are opened from navigation drawer.

I want that when back button is pressed in MainActivity to exit application, and when pressed on ActivityA or ActivityB to go back to MainActivity, no matter how many times are pressed list items from navigation drawer.

As far as I can understand, the problem is that all activities that are started from navigation drawer are added at back stack.

Also an extra question is how to highlight the item selected on navigation drawer when activities are changed?
I have done it in another sample that uses fragments with this code:

mDrawerList.setItemChecked(position, true);

Can I use Activities or do I need to convert them to Fragments?

Community
  • 1
  • 1
Endri
  • 714
  • 13
  • 34

1 Answers1

0

Creating new Activities on Navigation Drawer item click is quite uncommon. Use Activity with DrawerLayout inside and handle navigation click events by replacing Fragments. See also this tutorial:

http://developer.android.com/training/implementing-navigation/nav-drawer.html#top

Jerry
  • 295
  • 3
  • 8
  • I have used navigation drawer with fragments, and I know that what I want is not common, but I wanted to know if it is possible to do that. – Endri Apr 28 '15 at 10:00
  • It should be possible. Instead of replacing Fragment on item click in Navigation Drawer, make a new Intent and start Activity. However, this will create new Activity without your previous Navigation Drawer. When you click back button in this state, you will stop ActivityA (or ActivityB) and get back to MainActivity. – Jerry Apr 28 '15 at 16:26
  • my problem is when ActivityA and AcrivityB are pressed more than once, for example A, B, A, B etc. and in this example pressing back we got to A - B - A - MainActivity. Instead I want to go to main activity from the last activity that is opened, no matter how many times is opened before. Also I have tried to open activities A and B with the flag `FLAG_ACTIVITY_SINGLE_TOP`. – Endri Apr 28 '15 at 22:37
  • I still don't understand, how you can get to ActivityB when from your MainActivity you launch ActivityA. I suppose you can't get to ActivityB right from ActivityA, or can you? Let's say both A and B Activities are blank. From MainActivity in your Navigation Drawer you can launch ActivityA. Ok, then go back and then launch ActivityB. Then go back and you're back on MainActivity. What's the problem? :) – Jerry Apr 30 '15 at 23:14
  • From MainActivity we can launch both activities A and B. And from each of them we can start A or B. I have implemented navigation drawer and each activity extends it, so you can go to any activity in any time because navigation drawer is on all activities. – Endri May 01 '15 at 09:43
  • Ah, yes, I thought you may have implemented it like this. :) You basically have two options: 1. Call finish() method in both Activity A and B after startActivity(intent) method. 2. The other way to solve this problem is putting android:noHistory="true" inside of your ActivityA and ActivityB tag in AndroidManifest.xml. For example: – Jerry May 01 '15 at 10:59
  • Thank you very much. Both of them are what I needed. – Endri May 01 '15 at 11:17
  • "Creating new Activities on Navigation Drawer item click is quite uncommon" It is quite normal approach. – Marian Paździoch Aug 21 '15 at 07:52