Back to previous activity - android

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.

Related

Normal memory consumption for an android application?

My applications memory consumption seems rather high when switching between activities. I quick explanation is that I have an ActivityA that is singleTop and is basically the home screen, you select an item in a list and it takes you to ActivityB which is a more detailed version of the list item. You then press back and it finishes the activity returning to ActivityA. Both activities are quite rich in controls and have high quality images scattered around, but there's definitely something not right here.
I have drawn a quick diagram to show the lifecycle:
As you can see when the app first starts it is at 34MB, after selecting a list item it goes up 2.5MB, then when I press back it goes up again.. this will continue to happen whilst going back and forth. The first thing I need to establish is whether or not this is normal behavior for an application? If this is not the case I clearly have some work to do in releasing some resources, the issue is, what?
In my onDestroy() of ActivityB I unregister from all my receivers, I call finish() on anything I use and have even tried setting everything to null but still no change! I use no special flags when starting ActivityB or finishing it, could that be part of the problem? The history of the stack is sticking around maybe?
EDIT:
I use SceneTransitionAnimation to move an image between the two activities, when I turn this off it goes up 0.5MB each time I go from A to B and 0.01MB when I go from B to A. I guess I should ask the question of how the much bigger increase can be avoided whilst still using SceneTransitionAnimations??

Launch the Android App which is in No Longer Visible State

My issue is when an Android Application goes in the background. When I click the Home button and launch my app again from the home screen by clicking the Application icon, it should display the the same screen from which I went to the Home screen. But it calls the onDestory() method then comes out of my application. I thought the application is killed by the system because of memory requirement etc., but I need tokeep the activity and it should again show the same screen where I left instead of starting all over again.
This may be achieved like maintaining sessions.
try putting
android:alwaysRetainTaskState="true"
in the androidmanifest.xml for those activities, i think ICS does that by default now.
The Application will show you the same screen when return after "home" button if your app with different screens is made of different activities to show the interface parts... But if you just make some objects
visible=true or false
so, after resume you'll see the first view... Try to use Intents between different activities... And show a piece of code to help you... Maybe the problem is in overriding of onDestroy, onPause, onResume methods

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.

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