Maintaining stack of the android application's activities - android

Will be obliged if someone can help me with this.We were working on some android application,while designing its prototype we came across some issues.A little description of the application's prototype is as follows:
A Log in screen
a Home screen with logout at its top right
Every other activity is having a home button on top right corner.
Now going from one activity to the other in my application, I just called finished on the current activity and started the other(so there is no stack of the activities is being created) and when home button present on each activity's top corner is pressed i finish the current activity and move to the home screen.
More precisely,i can say that i am overriding every activty's onBackPressed() method
.By doing this i am not letting android to keep a stack of the activity's but by doing this I have a feeling that, I am loosing efficiency and degrading performance.Because on every backpress or home button click one activity is finished and the other is created.Hence some lag can be seen as the acivity is recreated.
Please suggest that should I continue WITH THIS or there is some other way out to handle this
Thank you for Giving your time

It is depends.
If you are giving option on each screen to to go home this approach is good.
Because if you are keep activities in stack it will be in RAM which is limited so it may create memory issue if to many activities in stack.
I would like to suggest one more thing.
As you say you have overridden onBackPressed() method in each activity.
Rather doing this there is a option you can specify parent activity in manifest tag.
So you no need to manually handle by overriding onBackPressed() method.
EG.
<activity
android:name="com.xxx.DetailActivity"
android:parentActivityName="com.xxx.src.ListActivity"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Translucent.NoTitleBar.Fullscreen" />

Related

Changing Activity screen when the app is not running

How can i change the screen of an activity when pressing any button on an android phone that takes one away from the running app.
I'm trying to get a blank screen to show up on the "recents" screen, instead of a snapshot of the app.
You can use this option and check if it helps you meet your need.
android:excludeFromRecents="true"
Add this to your activities in the application manifest if you do not want the app to be shown in the recent apps list. One drawback is that you would not be able to resume the app from the Recents list. Not sure if this is what you need.
The other way is to have a 'Blank Activity', which you start when your actual activity pauses. You can then finish the 'Blank Activity' on its Resume. This way, you will have your app shown on the Recents list, but with a blank screen.
You can call finish() for activity but that will kill activity. I think that will leave blank screen. Or destroy(). Try both respond with results.
you might want to change the onpause() or onclose() functions of your app. they are the last thing android execute before leaving,therefor you can change the aspect off your app just before you leave it
EDIT :
Maybe if you create PopupWindow and set it to full screen, and color black when exiting no preview would be shown, but app would still be running (idea of user DjDexter5GH) in the onpause() and onclose() functions. therefgor,when you leave,a black(or whatever you want) screen is pushed in front
you can then close it in the onrestart(is it called this?)

Back Stack, Splash and TabActivity

I've searched SO and found several answers to the question in general, and have tried them all and am not having success. I really don't have my head around how the back stack works, Intent flags or the finish method. Here's my setup:
On application start-up, there's a splash screen where a couple AsyncTasks run in the background and check a couple webservers for updated content. ProgressDialogs report status. When complete (via the last onPostExecute), I launch a new Activity ("Home"). This seems to reflect some of the other posts, but I think my kludge is due to Home being a TabActivity, with 4 tabs, that initially calls setCurrentTab on tab 0.
So, using the suggestions previously posted:
android:noHistory="true" on the Splash activity
calling Splash.this.finish() after it launches the Home TabActivity
setting the Home TabActivity intent flag of Intent.FLAG_ACTIVITY_CLEAR_TOP
setting the Home TabActivity intent flag of Intent.FLAG_ACTIVITY_NO_HISTORY
The users sees the splash, the TabActivity launches, the user clicks to another tab, then hits back - the application closes (not force close - just closes back to the devices home screen).
If I don't use any of those, when the user hits back after changing to another tab, they go back to the Splash screen and are stuck (I could add a button or something to take them to the Home TabActivity but that's not optimal).
The desired result is that the user sees the Splash, gets taken to the Home TabActivity, clicks another tab, then hits back, he should be taken back to the initially set tab (tab 0).
Any insight is appreciated.
TYIA
The back stack is actually officially called the activity stack - every time you start an activity, that gets pushed onto the top of the stack (unless you set one of those flags you mentioned).
This means that unless each tab in your main app is a separate activity, then the default back key behaviour will be to leave your main app activity.
You can control this by taking over the back key or by overriding the tab switching behaviour to start different activities.

Android Game Activity Flow - Back to Start

I am looking for the best way in which you would setup navigation in an Android game; a best practice for Android Game Activity Navigation. I have the following scenario:
A Main Menu Activity (Greets the User on Start and gives them options)
A RunLever Activity (This runs a level of the game)
A Transitional Menu Activity (This appears between levels and shows scores and stuff like that while providing a button to go to the next level)
Therefore, if the player starts the game and completes two levels they might have an activity stack that looks something like this:
The problem that I have here is one of expectations when the back button is pressed:
If I was on the last In Between Menu and I pressed back then I would expect to go back to the Main Menu. Instead it would probably take me back to the level that I just won.
I would not expect the last levels that I have played and previous In Between Menu activities to stay sitting there in the Back Stack. I would only expect the Main Menu, the Current Level I am playing and one instance of the In Between Menu to be sitting in the back stack at any one time.
As far as I can tell everything else will work properly and as the user expects. I guess I just want to know what the best options are to solve this kind of problem. To make sure that the activities in this game occur as expected. Should they even be separate Activities? Thanks.
Rather than pushing a new In Between Menu Activity after completing a level, can you instead pop the activity stack, and detect that (e.g.) level one has been completed when the already-there in between activity becomes active again?
That way, the first "back" would always go back to the in-between, the second "back" would go back to the main menu, and the third "back" would leave the game. All of which is probably what the user expects.

Kill all activities when HOME key is pressed android

I have an app which has 4 activities in it.Within app, history activities, i.e. Activities from where I navigated should not be destroyed , so I don't call finish() when I am navigating.
But when I press HOME button I want to kill all activities , So that When I come back to the app , Index screen or say first activity is displayed instead of previous paused activity.
Problem here seems to be, how to differentiate between backs within an app to HOME button.
I saw few answers regarding this in other questions. Got more confused.
Is there a way other than intercepting HOME KEY PRESS, because as suggested in other threads,
I should not override HOME key press (as it might have side effects)
Set android:clearTaskOnlaunch="true" on the activity launched from the home screen.
You might also check some of the other attributes you can specify on activity, to tweak it's behavior a bit more.
On main activity add:
android:launchMode="singleTask"
android:clearTaskOnLaunch="true"
On the others add:
android:finishOnTaskLaunch="true"
This way it will kill off any activity when returning to the app after being in background.

Killing activities before they stack, so recalling an activity doesn't cause an overflow

I have read into the finish(); commands and the FLAG_ACTIVITY_CLEAR_TOP commands and also checked out Common Ware's answer on killing app, but I am not sure how to put this into my app.
Basically, I have a user click a button that takes them to the camera. The user then snaps a photo and it brings them to a layout view. The user then clicks a button that takes them to one of 2 views, depending on a some conditions.
The user is then allowed to either retake a photo, or go to the main menu (depending). My problem is, if the user goes back to the main menu, and snaps another, then another, etc...the activities stack, so when I click the 'Main Menu' button the app goes back through eached stack activity until finally it goes back to the main menu. Is there a way to kill each activity with one of these lines, so even if a user retakes a photo, they will only need to go back once to get to the main menu?
Thanks!
I use the noHistory parameter in the manifest to accomplish this. Here is an example of a manifest entry for a Activity that should not be placed in the history stack:
<activity android:name=".MyActivity"
android:label="MyActivityTitle"
android:noHistory="true" />

Categories

Resources