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.
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've looked at a few questions on here and I never found an answer I like. I may not even need to launch two acitivies at the same time, but what I am trying to achieve is that on a button press, it brings up the calendar prepopulated with add ins for the reminders and to also move the view of my app to the next page. They'll see it when they confirm their reminder or cancel it and go back.
So really, the question is, how would I move my app to a new activity and also bring up the calendar with the info? I've been trying to do this onto one button to make it super simplistic, but it is not possible I can do it with two buttons...
Call the calendar activity from oncreate "next page activity". The "next page activity" won't load but will appear when going back.
If you don't want to always load the calendar from that activity you can pass a parameter and check it.
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"]
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.
Perhaps I'm not used to the Android development, but when I look, I expect everything to be done in order. When one activity finishes, the next starts. However, it seems that my code doesn't work that way. Take the following code for example:
for (int i = 0; i < 3; i++){
Intent myIntent = new Intent(Game.this.getBaseContext(), NextScreen.class);
myIntent.putExtra("something", i);
myIntent.putExtra("Opp", oppList.get(i).toString());
startActivityForResult(myIntent, 0);
}
It doesn't display activity one, wait for you to do what you do in that screen, then come back for the second activity. It immediately displays the third activity. If I click on the back button on my android emulator, it will show me activity two... and if I click back again, it will show me activity one... so it just kind of rapid-fires these activities onto the screen without waiting for you to do what you do in these activities. I'm sure I'm not the first person that's wanted to do something like this. Any idea what I'm doing wrong? How do you work around this situation?
Activities execute asynchronously. One way to serialize this is to chain the activites in your onActivityResult method. Pass to each (sub)activity an activity number, starting with 0 and have the sub-activity return it as part of the result. Your onActivityResult logic can deal with the response, then examine the activity number and fire the next activity (if there is one).
I don't believe you can queue up pages like this in 1 handy function. You'd be better off having a function nextPage which takes the current activity have moves the user to the next activity. This could be called everytime the user is ready to proceed to the next page (triggered by a button press or something).
Or just start the next activity on the button press directly