How do I go to previous screen from current screen in Android app? I know there is a back button on phone, but it takes me to beginning screen of my app and I want my buttons on app to work for going back to previous screen.
Back button indeed takes you to previously seen activity on screen, that launched the current one (not by means of back button). If back button takes you to beggining screen of your app means that navigation to your last activity was done from it. Try launching an activity from another one different from start activity.
What really can be problematic is ending application once at start activity by pressing back button and discovering the application switching to activity that lauched start activity (not by means of back button). In this case you should just call finish() inside onDestroy() listener method of your start activity.
Related
Hi Guys I have 2 activities, 1 is in Android Native(Extends Activity) and 2 is in LIBGdx extends AndroidApplication.
I go from activity 1 to activity 2 and then come back to activity 1 by pressing the back key, then I come back to activity 2 again. This time activity 2 is black and black in color.
Does it has anything to do with libgdx or its an android issue?
Thanks
This sounds like libGdx is losing the openGL context. libgdx will get this back automatically when the app goes out of focus (incoming phone call, user presses home key, etc) and then comes back into focus, but here the app never loses focus--only the activity does--so libgdx doesn't know to restore itself.
Does the user need to go back to the first screen? If not, I would mark the first activity android:noHistory="true" in the manifest. That makes the back button skip the activity, so the app itself will lose focus.
Otherwise, put debug statements in your ApplicationListener's create(), resume(), pause(), and dispose() methods so we can see how that life cycle is playing out.
I have noticed the splash screen does not display when user clicks the middle button on a real device to quit an application but if user selects back button from main menu to quit app and relaunches the splash screen will display.
Question is what function is the middle button performing compared to the back pressed and is there a way to ensure the splash screen is displayed when the middle button is used to quit an application.
Thanks.
The HOME button takes the user to the home screen (also called the Launcher). The BACK button by default finishes the current activity, which usually results in the user ending up on the previous activity he was in, until he reaches the home screen. The difference is BACK will explicitly finish an Activity, whereas HOME will not.
So I have my main activity, and when you press a button it launches activity2. Once activity2 is launched I press the home button and it will trigger onPause at activity2 and then the desktop of the android device comes to top. Everything's ok.
The problem is: now I click the app icon and instead of coming to top activity2 (the last active), it comes my main activity. So onResume is not called on activity2, but on my main activity.
Isn't it supposed to be called onResume on activity2? How could I make it work?
If you launch an application from the home screen launcher, you are specifically launching the activity which that launcher icon corresponds to.
The behavior you are thinking about is more along the lines of what occurs when you re-enter by way of an activity which is in the back stack.
If you want the home screen launcher icon to put you back in whatever activity you were previously in when that is different from the activity that icon is linked to, you'll need to keep track of that and have your launcher activity transfer you there.
Just to be sure, didn't you override the onKeyDown method which is able to catch device buttons?
Remove the android: launchMode="singleTask" from your launcher activity in the manifest file.
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.
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 for anyhelp.
You could use the noHistory flag which would end each activity once you're away from it.
Probably though, what you really want is singleTop launch mode, that will return to your previously opened activity rather than making a new instance of it.