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

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.

Related

ListView items - going Back and Forth

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().

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

Back to previous activity

I have a back button that will work exactly like that.
I know using back button is not a good android practice since android device have one. But my situation badly need it.
I cannot finish my activity and its tough to Keep track of the activity stack.
Since you know that overriding BACK button function is not a good practice, just follow it. Tracking activity stack and promising the BACK behavior can be solved by properly setting the launch mode of each Activity. (please see: Launch Mode in Android Official Site)
If you insist to do something like pressing the BACK button, it's super.onBackPressed().
its better for you to user super.onBackPressed()
Refer Go back to previous activity
I wonder why you wouldn't want to finish your activity. Could you move whatever it is in your activity you don't want to destroy into another object?
BTW: the activity will also be destroyed and re created if the screen is rotated.

Changing Activities within Tab

Alright, so I have an application that has a tabbed interface. What I would like to know is how I can change the currently active Activity in the tab, and then when I'm done with that Activity, go back the the original Activity, exactly as it was.
Would I be able to use something like a ViewFlipper for this? Or probably not?
If you're just concerned about what happens in the activity within that particular tab, the normal rules of activites apply, i.e. as if this activity were the only one in your application: You can either use Activity.finish() to go back to the previous activity on the stack, or use a ViewFlipper or something to randomly switch between various activities.
Another (discouraged) way would be to remove all views and then call setContentView() to inflate a new layout, but the above ways would be preferred.

Android back button does not restart activity?

My app intiates an activity. On the click of a button, the app opens up the browser with a webpage. When I hit the back button, it comes back to my initial activity screen, but does not resume or restart the activity.
When I put all the layout code and activity code in onResume instead of onCreate, the activity gets restarted.
My question is whether this is the right way to go about it? Can I use onResume to draw my layout and initiate the activity, or is this poor design? When the browser fires up, does the initial activity forget its layout?
Please let me know what you suggest.
Thanks
Chris
Mostly you should read about the Activity Life Cycle.
It is fine to initialize in onResume as long as you only do it once. Either have a dedicated hasInitialized member or check some other value that will have equivalent meaning, and do not initialize again if it is set.

Categories

Resources