When user click on a notification, I set up a backstack of Activities A -> B, where B is on top and shown to user. I would like the lifecycles of Activity A to run, so that when user presses back button and comes to Activity A, it is already ready. What could I do to achieve this?
This cannot be possible as android will not allow us to do that, the only way I can see you can achieve this by doing your Activity A operation inside your Activity B And provide those details to Activity A when the user pressed back button.
Note: If you are showing any kind of list on Activity A or doing similar kind of work, you can have one Singleton Class where you can get your data in Activity B which required by Activity A, and provide same data to Activity A when the user pressed back button.
You should open the activity as normal but then put this line of code in the OnCreate() method to bring to the back.
moveTaskToBack(true);
Related
I want to learn the proper way to manage the activity back stack with regards to my issue. Most of the time when a person uses my app, I want to keep an activity in the bottom of the stack, let's call this Activity A. This would be their "Home" activity. I have a navigation view which can take the user to a bunch of other activities, but I want to manage what displays when they tap back. I want Activity A to always be the last activity in the stack, so the stack can look like A -> B -> C-> D, and when the user is on Activity D and they want to go to Activity E, I want the stack to look like A -> E when they press it.
A possible solution I have found is by clearing all the activities in the current stack, launching Activity A, and sending an intent for Activity E in the intent I launch Activity A with, then that will just check it's intent extras and if it finds an intent in the extras it would just launch that intent. This results in the stack looking the way I want, Activity A -> Activity E. I just want to know if there is a better or simpler way.
I have tinkered with the activity properties in my manifest, but it seems like I can't do exactly what I would like to with those.
Any help would be appreciated :)
Lets assume you want to keep activity A in the back stack so that whenever a user presses back, you want to show activity A on top. Lets say you go to B from A and then C from B. So whenever you go from any activity(other than A) to any other activity just call finish() from the calling activity, this will remove the stack entry of the corresponding activity, ensuring that only activity is there in the back stack.
I have an app with a main menu at bottom; I can't figure out how should I manage the activity stack, because every button opens an activity, and each activity can start more activities, and i was looking for a management in the style of the current Instagram's app. It looks like (in the Instagram app) that every activity started by each button in the bottom menu opens a new activity stack, but when you press back button, it navigates in the reverse order you called every activity.
Sorry for my bad explanation, i hope that you can understand my aim.
You could check out Instagram Android app to figure out what is my goal.
My current implementation uses a MainActivity with a Fragment for the first menu button (Qui in giro->"Nearby"), but i probably should change this approach.
Thanks.
1) Firstly you will have to decide what will be your main activity.
2) Now use this main activity like as star topology. Means this main activity will be center activity.
3) Use to finish() method for finish back stack before receiving at this center activity.
4) After it if you open another side of your main activity than you will see only new back stack will be displayed after pressing back button for receiving at main activity again.
I get a screen navigation question.
1. From Activity A-> Activity B
2. on Activity B, click an action button to go back to Activity A (Activity B->Activity A)
3. on Activity A, click a cancel button to go back to Activity B (Activity A->Activity B).
I don't want to create Activity B twice. Is there a way to save Activity B at the second step? Thanks.
Is there anything big that gets initialized in B that is slowing the transition down? I'd just let the system do its thing, you can optimize state in an activity with onSavedInstanceState and onRestoreInstanceState.
a quick and dirty way to do that might be to save a flag in a static class and check the flag to decide what to reinitialize in onCreate method
In my android apps i have four activity A->B->C->D and in every activity i call web service then update the list-view and other element from the server so suppose i did delete some data from the database for Activity A and if user access the Activity B and he press the back button then Activity A should be refreshed. I did override onResume method and check the data is updated or not but didn't worked. I also tried another way but not achieve my goal.
my Activity structure is like a following
Class A Is Fragment which is having two tab and each tab contain listview
Class B Is Activity
Class C Is Activity
Class D Is Activity
I don't understand how to call web service again when press the back button and how to update fragment and activity when user press the back button. Please anybody suggest me some answer.
Thanks in advance
you can override the OnBackPressed-method. check the top-answer on that link!!
Android: Proper Way to use onBackPressed() with Toast
Start activity B from activity A with "startActivityForResult" instead of "startActivity".
In activity B you defaultly return RESULT_CANCELED -> so setResult(RESULT_CANCELED). Only when activity B is finished on purpose you return RESULT_OK.
In activity A you then just need to implement onActivityResult(). Depending on the result returned from activity B you can then do specific stuff in activity A, in your case refresh something.
Here is an example: http://www.vogella.com/articles/AndroidIntent/#usingintents_sub
I am new going for the android.I am working on an app in which I have two activities let say A and B.
In activity A I have a list view of some items.I have a button in activity A which takes me to activity B.In activity B I have a seek-bar.
I am using the seek-bar to filter the result of activity A.
I have two buttons in activity B cancel and filter.
After adjusting seek-bar if user clicking filter button than it takes user to activity A and showing filter results.
User can play between activity A and activity B.
Now three different scenario are there for coming back from activity B to A.
By pressing filter button
By pressing cancel button
By pressing phone's back button
After adjusting seek-bar if user pressing filter button then activity A is re ordering and showing filter results. Here I want to save the instance of activity B. so that from activity A if user again going in activity B then I can show the previous state of activity B.(I am able to do this)
In second scenario if user adjusting the seek-bar again and pressing cancel button then Activity A is reordering.Here I do not want to save the instance of activity B and if user again going in activity B from activity A then I want to show the previous state of activity B.(I am not getting how to do this ??)
In Third scenario if user adjusting the seek-bar again and pressing phone's back button then Activity A is reordering but now if user again going in activity B from activity A then activity B is restarting that I do not want, here also I want to show the previous state of activity B.(I am not getting how to do this also ??)
I am stuck with this problem.
Thank you so much in advance.
You must consider each of the cases and manage the activity lifecycle accordingly. You did this just fine, the problem is how to manage it. So, the first step is to study this:
activity and lifecycles
After understanding how the lifecycle of activities is handled by the android os you're on your way: Manage the life of your second activity so that state is mantained by overriding the onPause method (which is called when your activity is no longer in the front of the application) or finish the activity if you don't want to save the state (effectively calling the onStop method.
I would solve this in this way:
in activity A i call the activity B with a startActivityForResult. This way, when the activity B is finished, it's state is not mantained. So, call finish() inside activity B to return to activity A without saving it's state.
When wanting to save the state of activity b, call the activity A so that the onPause method gets called and the state saved.
Hope this clarifies it for you.