Android 3 Activitys - how to hande passing data? - android

So I just want to clarify the life cycles a little. I have three activitys A(main), B and C. B is startet from A with some extra infromation, that it needs to show the correct content. Now B starts the Activity C (no extra content needed).
Can now (Activity C is in foreground) activity B be killed? If so, when pressing the back button, do I need to transfer that same infromation from C -> B, that was transfered at the first creation of B? Basically, what I wnat to know is, if extra content is used to start activity from parent activity, should this same extra content be used to start the activity from its child?
Thank you
Jaka

No need to pass back data from C -> B when you press back button.
On B's onCreate, store the additional data into private class variables and you can use it when the activity is restored.

Related

Activity A->B->C->A. How to get B's data at onActivityResult of A

I have three activities, call them A, B, and C. A is the parent activity of both B and C. Here are all possible activity flows.
A -> B -> press back or up button in action bar -> A.
A -> C -> press back or up button in action bar -> A.
A -> B -> C -> press back -> B.
A -> B -> C -> press up button in action bar -> A.
Both B and C can modify data that stored in A. So I make B and C send the modified data back to A by an intent, and use A's onActivityResult to update those data.
The problem is in flow 4, if B modifies some data, how can I send those modifications back to A? Should I pass them to C and make C send them back to A while C absolutely do not need those data?
Update
I found that in flow 4. Activity A's onActivityResult do not receive the returned Intent, the Intent parameter is always null, so we can't pass any data from C to A.
Originally I think A will receive C's Intent, so I can pass B's data to C then to A, but it's wrong. Activity A do not receive any Intent in flow 4.
Now I decide to change the up button's action, make it same as back button, so flow 4 will gone.
There is no problem in flow 4.
just need modifies data in B,no need to pass data to other activity.
call setResult() before B finished like other flow.it will return to A successful.
This is not the direct answer to the question, but recommendation :
When there is high interaction between Activities or need of too many activities, instead of too many activities or activity transactions , try to use Fragments.
Using Interfaces (same/similar to JAVA) is the magic wand of the fragments.
Use of fragmentation makes coding as easy as swapping the pages literally (Damned useful).
Here are the useful links : (You can copy, paste this code and can manipulate according to your requirements)
https://developer.android.com/guide/components/fragments.html
https://developer.android.com/training/basics/fragments/creating.html

Android three activities, pass data from third activity to parent activity

I have 3 activities. Let's say A, B, C. Main activity is A.
I'd like to start B from A then start C from B, then close B when C opens. Finally upon closing of C pass data to A.
How's this possible especially with Intent and startActivityForResult? Is it at all possible with the two latter methods?
Thanks.
Supose you are using a button, when you click the button to start the activity C, you can finish the activity B, and get the action in the activity A.
Use onActivityResult() on activity A. On this pont, you can start the activity C using startActivityForResult().
This way, when you finish C, you can pass data to A, again using onActivityResult().
Renan Lopes Ferreira's reply did the trick. Cheers.
You can always start B from A and C from B using Intents.
As you are removing A from the stack, C cannot directly pass data to A using startActivityforResult() and onActivityResult). (Isn't it obvious, A doesn't even exist in memory).
So for A to access C's data, you must store C's data somewhere, so that A can access it, the next time it is up. Now, here the other scenario would be store it as persistent or non-persistent data. If you want the data to be persistent, it is recommended to use Shared Preferences, Database etc. and if you want it to be non-persistent then you may use Singleton etc.
Refer : http://developer.android.com/guide/faq/framework.html

Killing Activities using Back Button

I'd like to kill all activities when the user hits the back button and returns to my main activity A. My App has the following 3 activities:
A -> Displays users selections before they hit search and creates activity B.
B -> Displays a list of urls's based on the users selection and creates activity C.
C -> Opens a Webview and displays the page selected by the user.
Currently, i don't call finish() after starting activity B so when the user hits the back button in activity C they can return to the list of url's and make another selection if they want.
I'd like to create a new activity A (without the users initial selections) and ensure the existing activities B and C are killed if the user hits the back button in activity B?
Any help would be appreciated.
Thanks
O.
You can return to activity A by overriding the back button handler in activity C and launching activity A with the flag FLAG_ACTIVITY_CLEAR_TOP set, or alternatively (perhaps preferably in your case), you can just set noHistory for your activity B.
You'll have to reset activity A manually in some way in this case though.

Android - activities lifecycle

lets imagine this application like enclosed image. R stands for Root activity and A and B are other activities. A is for displaying of some list, B stands for displaying detail of some value. From R I can get by 4 buttons to 4 A activities. My question is, whether in this scenario is A activity initialized for each time or I would be using only one A activity among whole application. If user selects A in right top corner activity A, then displays detail B and then from detail B goes to another list (but based on A activity). Will he still got the same content from the first A or he can have "new" A activity with another list?
Here is the point - I will be having let's say hundred of activites. Lot of them are "forms" displaying some content, application will have about 50 variants of those forms. Can I make for each form one activity a reuse it again in my activity without having connections to past usage of this activity?
Use android:launchMode="singleInstance" in your manifest if you want a single instance of your activity class
By default activities start over entirely every time you open them with a call to startActivity(). If your activity starts a new activity, it will essentially be put on to an activity stack and is paused while the new activity starts. If the user presses the back button, the last activity on the stack will resume as it left off (though I don't think this is 100% guaranteed as Android will kill off tasks as it needs resources so I wouldn't assume it).
So if you have this chain here:
A->B->C->D
where each letter represents a new activity with absolutely no flags or changes.
If the user is at D and presses the back button, C will resume. D is popped from the stack. If the user decides to go to activity D again, a new D will start as if it didn't happen (assuming you didn't save any persistent variables). If the user presses back twice, the application will be at B where C and D are no gone.
You can manipulate this chain with various flags like singleInstance to keep its state, or noHistory to ensure it doesn't get put to the stack (meaning it will be skipped if the user presses back).
Very detailed description of the various attributes

Android: Get already displayed activity on front, not by calling finish() on current activities

I have an activity A, which invokes Activity B And then upon clicking buttons It goes to Activity C and D.
In activity C and D there is a button called Back. And on click of back button, activity A should come on front with same data that was shown previously.
More Detail: When Activity A is first called it receives some bundles to fetch title and user name to show on the screen, remaining values on textviews on screen is fetched based on these two values.
Now the question is:
Is it possible to put Activity A on front with calling intent and without putting extras on it?? Is there any OS(2.1 onwards) supported mechanism that shows me last bundle data if there is no new bundle passed??
You can keep the bundled stored in a local variable. When the activity goes in background, it is not always destroyed. So if you do a startActivity(A) it should bring activity A in front again, without restarting it. And if the bundle is empty that means Activity C or D has called this Activity. Also do check the launch mode for some details regarding the Activity's behavior.

Categories

Resources