ListView items - going Back and Forth - android

Ok, I have a ListView inside a DialogPreference that is populated with an CustomAdapter extending BaseAdapter.
When the dialog comes up the first time, it shows a list of Root directories that I get from a web api. Once the user clicks on one of the ListItems, I now show the sub Directories.
Before adding the new listview I do this to clear the current one:
lv.setAdapter(null);
I would like to have sort of a breadcrumb where the user can see in what directory he is, and can easily click on one of the crumbs to go back to that directory.
Root > Channel > SubChannel > SubSub > Foo
Im not sure how this would or should be done. I would just like to go back basically, as a back buttons would have done (although im not using the back button in this case).
Thanks for any suggestions in the right direction.

How are you handling navigation now? Is there a set number of levels to the hierarchy, with a different activity at each level, or are you performing all navigation within a single activity?
If each level is essentially just a folder indistinguishable from any other, you'll most likely want to override onBackPressed(). If you're at the top level and want to exit the activity, call super.onBackPressed(); otherwise, handle the navigation yourself and don't call the default implementation.
If you have a different activity at each level and want to be able to go back an activity from your breadcrumb bar, you can call finish(). If you need to go back more than one level you will have to communicate to the previous activity that it too needs to call finish(). The best way to communicate between activities is to use startActivityForResult(), setResult(), and onActivityResult().

Related

Whether or not to use multiple instances of an Activity on Android?

If I have an app that refills a ListView with different data in response to a user selecting an item. Should I start a new Activity or just change the data, and call Adapter.notifyDataSetChanged()?
It seems like calling notifyDataSetChanged() is simpler, and would minimize my potential number of activities (and memory usage), but I want the Up/Back navigation to work.
My app works like File Explorer where there is a list of folders, and clicking on a folder changes the list to show the contents of the new folder. If the user clicks on a folder, I want the Up/Back buttons to take them back to the prior folder. I have implemented this with a single instance of the activity and using notifyDataSetChanged(), but can't get the Up/Back buttons working as desired. I am thinking I need to either override those somehow, or use multiple instances of the same activity. Any direction here would be appreciated.
Thanks
Using multiple instances of the same Activity is highly unproductive.
To solve your problem, the best way to do it would be to use a fragment using multiple instances of it, each one representing a folder in your arborescence. Then in the onBackPressed() of your activity, you can just
use popBackStack() while there is a fragment in the backStack, then call super.onBackPressed() to resume default behavior.

Android: How to switch between activites properly with navigation drawer and child activity

Basically I came across a problem that I do not even know how to search / look up So I will try to explain it as best I can in as little words as possible.
In the project I am working on, I have a sliding/navigation Drawer(MainScreenActivity) that I use to switch fragments without using new activties. However, when I click a menu button in the action bar I go to a new activity which lists notifications of people " checked in ". The user than has the option to accept the "check in" by clicking a button. When the button is clicked, this should directly go to one of the navigation/sliding drawer Fragments and bring it up. How do you go about doing this.
If there is any confusion I can try to clarify it.... Thanks
Navigation/Sliding Drawer"Record"Fragment (MainScreenActivity) - > (NotificationActivity) - > "Record" Fragment" (MainScreenActivity)
The common way to approach this issue is to create listeners in your activity and within your fragment you can use:
YourActivity.class.cast(getActivity()).calltheListenerForThatFragment();
You can add different listeners for the different fragments that might need to call the activity or one single listener that takes an int as parameter:
YourActivity.class.cast(getActivity()).callGenericListener(fragmentId);
In this case you can make a switch in your activity listener and separate the functionality depending on the fragment that called it.
Hope it Helps!
Regards!

Best practice to display nested lists on android

I would like to display a tree without using the tree component, since I don't know how many levels there will be.
I though about a list, that opens another list on item click, and so on...
Is it correct to open a new activity for each level?
Will it not overcharge the system?
Or is it preferable to have only one list that I clear and refill with children?
How the standard back button will behave on both case?
I would have open a new activity for each level. Unless your tree is extremely deep, it will save you time handling back's button correct behavior by hand if you do it with only one activity.

How to manage multiple Activity stacks in an Android app, like you would on iOS with multiple UINavigationControllers within a UITabBarController?

A fairly common model for iOS apps seems to have a single UITabBarController, where each tab essentially holds a UINavigationController, i.e. a stack of controllers, and pressing a tab switches to the top of the corresponding stack. Can I get the same behavior on Android without a lot of custom code? (Note: I am using menus instead of tabs on Android).
After reading http://developer.android.com/guide/topics/fundamentals/tasks-and-back-stack.html the closest I can see is to have multiple tasks, each one representing a stack (akin to UINavigationController), and to use FLAG_ACTIVITY_NEW_TASK to switch to another task/stack. When I switch, is there a way to go straight to the top of the stack, or do I need to keep that piece of state myself?
One problem with keeping that state myself: I've noticed that if my app launches an Intent that starts a new process, sometimes my original app's process is killed (I think), and all of my global state is destroyed.
The only other solution I can imagine is to have a dummy Activity per stack, to push DummyActivityN essentially right before I switch away from the Nth stack, and, when switching to the Mth stack, to start activity DummyActivityM with FLAG_ACTIVITY_NEW_TASK, and then have DummyActivityM immediately finish() itself.
One last problem: as I navigate to the bottom of one of the stacks, with the back button, I would like to hit the back button once more and NOT go to another stack/task. But this seems easy to overcome by launching an Intent to go to the home screen; is there anything better?
If I understood your problem correctly, if you add this parameter to your Activity declarations in AndroidManifest.xml, the Activities that are launched by your custom menu will be only created once - keeping their state while you move around the tabs.
android:launchMode="singleTask"
I hope this helps,
Best,
-sekran

Make new activity take the place of the parent in the stack

I have read about activities, stacks and launch modes, but have a hard time understanding how to apply this info to my specific problem.
Basically what I want to do is to launch a new activity, and make sure that it takes the parent's place in the stack, rather than being placed on top of it in the stack. I have the following two scenarios:
I have a login-activity. When the user logs in, a new activity is launched. When the user hits "back", I don't want him to be sent back to the login-activity, but rather to exit the program.
I have an activity that is displayed in a tabhost (or rather in a framelayout inside a tabhost, I suppose). This activity has a button, an clicking on this launches a new activity. I would like this new activity to take the parents place. That is, i don't want to open a new full-screen activity, but instead I want it to take the parent's place inside the framelayout in the tabhost. Also, I don't want a press on the back-key to lead the user back to the parent activity.
I would appreciate it if anyone could point me in the right direction. Thanks in advance.
All you need to do is call finish() in your Login Activity. That will remove it from the Activity Stack.
This is a more difficult answer. My best suggestion here would be to either use a ViewFlipper and instead of starting a new Activity you can just manage switching the views out. Alternately, if you need the button to start a new Activity, you could use an ActivityGroup and manage the Activitys that way. They both have their own complexities, so you'll need to investigate which may work better for you.
In the first case, you need to call the finish() in your login activity.
Also check this manifest settings to finish the activity when a new task is launched.
http://developer.android.com/guide/topics/manifest/activity-element.html#finish
In the second case you may use two views as frames or a ViewFlipper. Then overwrite the keyevent to handle the back button click.
http://developer.android.com/reference/android/view/View.html#onKeyDown(int,android.view.KeyEvent)
Check if the user clicked when they are in the second view and show your first view. And if they are in the first view you may want to return false on the method that you overwrite to let the system to handle the event. [Probably close that activity]
Alternatively for the second option, it might be possible in some cases to just remove tabs and use the options menu. This way you can use your activities in a normal way.

Categories

Resources