I am writing a android application where, on startup activity view, I have a button "DoIt". When user clicks "DoIt" button, I start another activity with different layout. On newly started activity, I have a button "Back" which should take me to the first activity. How to accomplish this. What code should I write on OnClick method of "Back" button. Also, I want the newly created activity to die after back button is pressed and application comes back to start-up activity.
In the new activity you can just call
this.finish();
to return to the previous activity. If you want a result from the child activity you have to launch it with startActivityForResult() and override onActivityResult in the parent. The hard back key should always go back to the parent activity by default.
Call finish() on your activity. Also, why are you making a button on screen for this? This is usually the job of the device's back button.
In my opinion, Android is really bad on such scenario. In Activity, it doesn't support multiple views. Consider the situation that users want to switch from these two views, or even several other views? I think in this case, iPhone is much much better.
Related
I have used the concept of Fragments in my Android App. I want to close my App from a fragment Activity. It's easy to do it in normal Activities by using onBackPressed() function, but how to do it in Fragments?
Fragment never runs independent. Activity contain the fragment so if you want to go back just press back button.
If you have any button in fragment to do so then write getActivity().finish();inside button click method.
EDIT 1 if you want to push your app in background then write following code
getActivity().moveTaskToBack(true);
The user can quit your app by pressing the Home button or navigate using the Back Button. Having a button in your app which closes it goes against best practices suggested by Google.
I've got an app with two screens, we can call them List and Details.
If an user is at Details and presses Home to minimize the app and then switches back I want to stay in the view and just restore, but if he presses Back I want to go back to List, I figure I can save a "Done"-button this way. But...what's the proper way to do this?
Currently I've overriden onPause and onSaveInstance but it seems they're both called in both cases.
I'm thinking about overriding onKeyDown instead, like he did; How to control Activity flow - Back button versus Home button, but that doesn't seem like a "nice" way to do it so I thought I'd check if anybody else has another idea.
Make two activities, for list and for details. When you will press the back key in the details activity it will finish and will show up the list activity.
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.
I created an Activity which allows navigating between pages with a couple of Buttons (Previous and Next). When the user clicks one of the buttons, the Activity (same) needs to be "refreshed". In order to do this, I set up the buttons to make a call to
onCreate(this);
after they set up the other stuff that the activity uses for the paging to work.
And it is working so far, but I'm wondering if there is a better way. Is there?
You should rethink your approach. Having Previous and Next buttons looks like an iphone's NavigationBar View. Remember that in android you have the back button, so a previous button shouldn't be necessary.
If you wish to do it, check Android's Activity Lifecycle. You should update your Activity on the onStart() method and avoid the call to onCreate(this) which doesn't sound good.
The activity displays data related to a certain day. The navigation allows choosing the day which is being displayed. As such, clicking one of the buttons changes the information which is displayed.
Example:
When the Activity first loads, it shows a given day - let's say August 23rd. Then when the user clicks the activity's "Previous" Button, the Activity shows August 22nd.
Then the user clicks the "Next" button and the Activity shows August 23rd again. If the user clicks the "Next" button again, the Activity will show August 24th.
Doesn't it seam a little strange to only have a "Next" (and rely on the physical "Back" button) ?
Thanks.
I fixed this trough refactoring:
Created a new function with the code that was being executed in onCreate (i.e. Layout initialization, database access and information display). The new function is called by the onCreate method (when the Activity gets the "Create" notification) and at anytime that I need to "refresh" the contents of the Activity (i.e. when a button is pressed).
Thanks for your comments.
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.