When my program starts, it runs a mainActivity, which right away launches and loads data into another Activity. I do this because the data needs only be loaded once, in mainActivity, then sent along to the other activities!
So! With that being said, my problem is probably forseeable! When the user clicks back enough, he will get to this initial activity, and sit there... ungracefully, instead of going back to Android's main menu.
What I want to do is add something to the onResume() method of the mainActivity, so that when it is re-entered I "activate" the back button from within the code-- (a boolean flag makes sure I'm not doing this incorrectly, thus I know it was on a back click). So isn't this just popping the current Activity from the Activity Stack?
how do I pop an activity from the activity stack!
Thanks!
Instead of using the "noHistory" attribute,you can also call finish() as you launch your next activity.. It will not re-appear when you then hit the "back" button.This will close the first activity.. removing it from the stack..
Use no history option so that this activity never gets onto the backstack.
android:noHistory=["true" | "false"]
Related
I have two activtiy. When a boolean variable in activity 1 is true then the UI in activity 2 should be update (activity 2 is with fragment). When I back to activity 2 with (setDisplayHomeAsUpEnabled) it works correctly but with back button it does not work. What is the difference between this ways and how I can solve this. I try update the activity 2 with this code but it does not work:
#Override
public void onBackPressed() {
super.onBackPressed();
//update();
}
The Home Button is not meant to behave like the Back Button. See this reference. The Home or "Up" Button should take the user up within the view hierarchy (i.e. if you have a number of multiple choice fragments, don't go back through each of them, but back to the activity that started the first fragment)
Each activity should specify what it's parent activity is in order to properly use this functionality. You can do this through the manifest, or by overriding onOptionsItemSelected() as outlined here
In terms of why your activity 2 may be updating when using the "Up" button but not the back button, this could be because both handle the back stack in a different way. Back will take you to the last thing on the stack and resume it, assuming you don't haven't tagged the activity with a launchmode that alters this behavior. Here's more info on the back stack. If you haven't designated your Activity 2 as "singletop" then when you use the "Up" Button, a new instance of that parent activity is created. See here. This may means that the information is updating after using the home button because it's creating a new instance of the activity, while for the back button - you are not creating a new instance but resuming a previous instance... make sure you implement an onResume() function to properly handle this and update the information.
I often see examples of classes which end with finish(), but definitely not always. My question is when should you end a class with finish()? And what does it do exactly, what is the difference between ending a class with the back button and ending it with finish()?
Thanks in advance!
finish() can be called to kill (destroy) an Activity instance. If you don't need to close your Activity manual, which is true in many cases, you don't need to call this method.
But if you require a button somewhere in your activity that says "close", then you should use this method. But in general the back button behavior in Android will handle things like this.
The back button does not actually finish your activity, finish() calls the onDestory() method right away, while the back button does not.
When the back button is pressed, the onStop() method is called, but the onDestory() method call might be delayed by the system, this so that the Activity can be resumed by the system which is cheaper (in resources) than a full restart.
Lifecycle:
http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
Finish():
http://developer.android.com/reference/android/app/Activity.html#finish()
You finish Activity class with finish() method when you need to end the Activity immediatly at specific point. If you press back button, you will go through the lifecycle (onPause(), onStop() then onDestroy()) automatically.
One reason to use finish is when you have started the activity with
void android.app.Activity.startActivityForResult(Intent intent, int requestCode)
In this case you dont go back to the Activity which started it with another startActivity but with finish
Back button takes you to the last activity in the stack whereas finish() destroys the current activity. You can call finish() on an activity for which you don't want the user return to. Example: LoginActivity
public void finish ()
Call this when your activity is done and should be
closed. The ActivityResult is propagated back to whoever launched you
via onActivityResult().
finish() is called to kill your activity instance after you did what you have to do with the activity. After that, you have no use with the activity. So simply call finish() and it will kill your activity.
If you press back button, it will call finish() automatically to kill the activity. It will go through the activity lifecycle(onPause(), onStop() and finally onDestroy())
The one reason with calling finish() is that, if you don't call it, when you navigate through activities, all the activities will be added to the backstack. So when you go back, you have to come back through these activities. So for e.g when you click back button, it simply kills the activity and it will be removed from the backstack.
I understand the concept of the back-stack, so I'm pretty sure this isn't possible but thought I'd ask anyway.
If it isn't, then what is the approach to simulate this behavior? For instance, I have an Activity "A1" that starts another Activity "A2". "A2" alters the content that "A1" shows. When the back button is pressed, the old "A1" is displayed with the old content. Whenever "A1" is called again then the new "A1" will show the new content.
How do developers get around this issue?
When a user press the back-button the A1 activity comes back to the foreground. This will not trigger the onCreate() so you can't use that but if you look at the Activity Lifecycle the onResume() method will be called.
So if you move the displaying of the content in activity A1 from the onCreate() to the onResume() method it should work fine in both situations, when the activity is started and when you return to the activity using the back-button.
Check the Activity Lifecycle. When an activity is made visible it will go through onStart and then onResume when it gains focus. You can load new content at one of these points instead of in onCreate if you would like to update whenever the user navigates there.
If I have Activity A and Launch Activity B from it, then press home I go back to home screen. If I launch the activity again from the recent apps, it goes back to B as it should, but when back is pressed the Activity send me back to the Home screen not Activity A. Activity A and be are alive for the entire time. How can I make the back button function as expected? Thanks.
Don't call finish() on Activity A if you want the back button to go from Activity B to Activity A...
Pressing the Home button empties the Activity stack.
You can capture the Back button and make it do what you want, but you would have to implement your own stack of previous activities.
I have two screens, in second screen i have one back button that shows the first screen(using intent). if i click that back button from second screen i navigated to first screen no probs in that.. now i want to navigate back to second screen automatically without clicking any button. Please help me how can i do this. Thanks for ur valuable time .
I would give a try to a ViewFlipper activity, with two screens. This, of course, if you can encapsulate your actions into one activity (which i'm sure is manageable with AsyncTasks / Threads, Services and Handlers).
This way you can use gestures, (soft/hard)buttons, dropdowns, -whatever-, to navigate between your screens.
You can automatically go back to a previous activity on the Activity stack by calling the finish() method.
If you want to do this without any user interaction, use a Handler that automatically calls finish() on your activity after a predefined timeout (see this article for more information on Handlers).
try with Thread.sleep([ms]) and then call finish or call intent to the first
The one solution is, when you press BACK button from second screen instead of call finish(); you should start first activity again, so the activity stack will be
FIRST
SECOND
FIRST
so after the delay of two second your activity will finish and eventually will come back to second screen, and state will be maintained also.