I am totally new to android development....what piece of code shall I write to move back to the last activity from the current activity on a button click?
I use intents to switch between the activities but is there anything specific to resume back on the last activity?
Please advise...
Thanks,
Pressing the back button should do that "out of the box"...
If the user clicks on Back button, application will automatically take you to previous Activity. You don't need to do anything.
If what you pretend is to implement that action from current Activity, for example through a Back menu option, just call finish() in your Activity. You can call setResult before that, if you called that activity with startActivityForResult
Ger
By default back resume previous activity.
You should read this to better understand android Activities:
http://developer.android.com/guide/topics/fundamentals.html#lcycles
Related
i'm trying to show an add on my app .
i want to show the add every time that the user launches the app or if the app is in the background when the user relaunch it.
i added the function that shows the add in the onRestart() event and it seems to work fine.
my problem is that if the user goes back to a previous activity by pressing the android Back button it triggers the onRestart() event as well.
is it possible to determine if the activity was loaded by the back button?
Thanks alot
Avi
From what I understand, here is what you can do:
If you are starting an activity from another activity, instead of calling startActivity(), use startActivityForResult(). In the called Activity, override the onBackPressed(). Put some data in the returned intent and call super.onBackPressed(). In the caller activity you will then analyze this to know that back was pressed :)
Also, move the advertisement to the onResume() method which is called when the Avtivity becomes visible to the user.
I suggest you to put your add in the Onresume() method of your main activity;)
I'd like to destroy an Activity when the user clicks on a button (not the back button, a different button). I've decided to just call super.onBackPressed. Is that okay? What's a better way to close the current Activity without closing the whole app?
for closing an Activity you can use finish() , but you have to aware of back stack, if you finish your last activity in the stack so there will be no activity in your stack and you must restart your app .
see more information on : Task and BackStack
The default implementation of super.onBackPressed finishes the activity.
Instead of using super.onBackPressed , Better way to close the current activity will be to call finish() method.
Also, When you press the back button, the onResume method is called.
Use onResume Method and refresh your stuff there.
Also, if it is listView refresh the data -> https://stackoverflow.com/a/12662994/2006412
Pseudocode :
The initial Activity starts. (ActivityStartScreen)
A button is pressed that starts a new Activity (ActivityOtherScreen) by using an Intent.
The new Activity has a button that loads the initial Activity.
My question is, is the original activity gone once it 'loses focus' (when the 1st button is pressed), or is it stored somewhere, and is there a way to retrieve it?
Currently, Im re-creating the original activity with an intent. I bet this isnt the proper way.
The previous Activities are stored in the Activity stack, to return to previous activity, just call finish() on current activity. Note that this way you lose the 2nd activity.
You simply must call the finish() method from your second activity to go back with the states all being the same.
Here is a helpful warriorpoint blog post tutorial that will walk you through it.
Right now in my android app a user presses a button to go to another activity, and then must press the back button on android to return to the previous activity. Can I have a button on my app and write code to go to the previous activity?
As the easiest way, you code write a code where your current activity is closed on a particular event, say on a button press. the method to be called is finish()
This way, when your current activity is finished, you are taken back to your previous activity.
Yes Ofcourse. : See the vogella article
I have not really understood the handling of activities and the stack.
I have 3 activities, A - a splashcreen, B- a menu and C another Activity. I start the splash and exits it after a while when the menu is started.
In code I handle them all like this:
startActivity(new Intent(this, ContactInfoMenu.class));
finish();
Now, if I start the app and goes A-B-C, when I hit "Back" in C screen I jump back to B-the menu. Another "Back" exits the application, just like I want.
BUT .. if I go A-B-C-B-C - the "Back" button in C screen exits the whole app instead of getting me back to the B screen?
Why is that? It does like that in all my "subscreens", I can only enter them once, if I enter them a second time the "Back" button exits the app. And I have not tried to catch the "Back" action anywhere? Shouldn't I always call "finish()" when I start a new activity?
Regards
Finish is good for leaving the current activity and going back to the previous one. Otherwise, try to avoid calling finish() if you can help it.
There are a set of flags that you can pass when you start an activity that do a better job of determining how that activity behaves on the stack. These include:
FLAG_ACTIVITY_NO_HISTORY - your activity will not remain on the stack after another activity covers it.
FLAG_ACTIVITY_CLEAR_TOP - a good way to pop off a bunch of activities when you need to "go back" to a certain activity.
Many of these flags can be set in the manifest. Reading up on them will give you a better idea about "The Android Way".
Basically, You don't need to call finish() every time you go to another activity. If system is low on memory it will close your activity instance by itself.
finish() is more often used when yor are inserting some information in one page and then moving on to some other page. In that case, you might need to fininsh your first activity.
But in case where you need to shuffle between views, you must not use a finish() function, because it will cause the whole application to finish.
Try using back button of your own in your views to shift between activities, where you can move to any other activity of your application or even to the Main Screen.