Save data between multiple activities - android

First of all, sorry for newbie questions..
In my app I have two activities.
Activity A is in general a registration form with edittexts, autocompletetextviews, spinners and two buttons X and Y which leads to Activity B , practically a map activity. Applying startActivityForResult for button X I go to Activity B taking some data, then back to A ( onActivityResult ) then button Y leads with same procedure to Activity B and finally back to A.
The problems is that Activity A doesn't remember results from button X procedure when button Y ends its own.
Question1. Filling edittexts in activity A, going to activity B and back to A doesn't prevent activity A to hold this kind of data contrary to startActivityForResult and onActivityResult . Why is that;
Question 2: What is the proper way to save data from button X in activity A so that activity holds results both from the two button procedure when button Y finishes its own;
Thanks in advance..

#FKSI is correct that the proper way to do this is using Intents Extras. However, I use Several Fragments inside each activity and when I have a mix of many activities and fragments all playing together AND if the data is not a lot, I find it very easy to use a SharedPreferences. Using getApplicationContext() as the Context, saving any SharedPreferences is accessible from anywhere in the package of the app.

The proper way to send simple data between activies is using extras. Have a look at Android official documentation

If I dont misunderstand, you just want to save temp data. I recommend you to use Application class. An example can be found here: Example. Hope it helps.

Related

How to change views in android

Consider we have two Activies: Activity A and Activity B, both have an ImageView. If we click on ImageView in Activity A, ImageView in Activity B should change to another image. Please tell us, how to achieve this by using interface
Your timely suggestions will help us a lot
Send some info about the clicked image in Activity A to Activity B using Intent. Then based on that info show the corresponding image in Activity B
As i understand what you are asking for has been asked many times. Its how to send data between activities. Here is an answer from another thread. Also you can check the Android documentation and read about bundles.

keep data retained of listview when switching between activities

i am struggling to keep data or items of list view when i leave one activity to another, for my example i created simple app on click will add number increased to a list view so each click create an item like 1 another click add 2 and so on.
the program works fin for main activity but then i would like to see the result on another activity i call it second activity but the problem is when i hit back button on second activity to go back to main activity, i will lose all of items on the list view.
i was looking on google so many information but could not get my head around it, so please advice and please be little more in detail as i am beginner, i think instance state or shared preference will do the job but i do not know any of them
thanks in advance and here is my app code for main activity and second activity and picture for output
sorry i add code as images becausethe site keep saying the code need indentation thank you
main activity[main out put][2]second activity[second activity out put][4]
You need to save the data of the ListView in some form, either in a file or in a database (local or remote).There is no direct way to store list view, but you can store the data from the list view and then set it in to the ListView later when you switch back to the activity.
You need to keep in mind that switching activity results to invoking of onPause() method in android, and if the data is not saved in the current activity then it will be lost when you move on to another activity.
Add all your values into the array, pass it to the adapter, then after clicking on the list view item, make an intent where you want to switch your activity (means from one activity to second activity), while implemented the intent, pass your array also, using intent.put extra. Then you will get your array in your second activity and coming back to your previous activity, again pass an intent (with your array) and get your array in your previous activity and pass it into the adapter.

navigating between 2 activities in which one of them is graphics heavy

I have this situation that I am not sure about the right design/way of doing things.
I have an activity where user would spend most of his time (Call it Activity A). Then the user can go to another activity where it is more graphics intensive (Call it activity B). Activity B would have around 40 Imageviews that have looping drawable animations. The user will be navigating back and fourth between those activities multiple times.
Is the expectation to create Activity B every time the user navigate to it and reinitalize the the 40 views based on the stored Data in my application class (it has coordinates and the type of view created)?
Or is there better way?
Thank you
You can use Following flag with intent to resume same activity without recreating it:
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
Instead of using activities, you can use fragmentA and fragmentB
When you switch between two fragments, data of them will not be lost.
learn about fragment here
you can use fragments with in the single activity. fragments are easy to swicth between another fragment. so did not lose data of another fragment.

Android loading image from Gallery - activity flow

I have two activities, Activity A has a button "Load image" which should let the user select an image, but I want to process the chosen image on an other activity B.
I think this is a common scenario, how should I handle the activity flow??
This is what I came so far(which I don't like)
Activity A--> Gallery --> Activity A --> Activity B
Activity A's onActivityResult() only gets the URI and starts Activity B, so It kind of a waste to recreate the activity just to execute 5 lines and be destroyed (insn't it?). I would like something cleaner or more direct. This is just an example, anything is welcome:
Activity A--> Gallery --> Activity B
EDIT
What about this?
Activity A--> Activity B--> Gallery--> Activity B
Is it a better approach or is it the same thing? I mean, will i gain something (performance, cleanness).
EDIT 2
As #Gaurav said, a workaround option would be to use 2 layouts instead of 2 activities thus no need to directly deal with my problem. I must say this should do for me as activity A is very thin. But for the sake of knowledge, i will welcome a "direct" answer to my question.
EDIT 3
Finally, having both layouts in the same activity didn't work (though it was a very attractive solution). My Activity B is based on libgdx (opengl), and i'm getting some nasty deadlocks when onDrawFrame is not called. So to avoid future untraceable bugs of this kind, I'll separate my activities. I'm going for Edit-1 solution, any comments would be appreciated.
I think there is no other way than to call Activity B in the onActivityResult of A.

Activity Navigation advice please

Ok . . . I have read hours upon hours of advice and forums and developer pages to no conclusive answer.
what I have: is an app with 8 activities and each has a slightly different set of buttons to navigate to some of the other activities. Each activity has multiple forms of data collection, ultimately ending in an e-mail intent which collects all application UI data.
Problem: I cant seem to find the proper way to use the navigation buttons in conjuction with onSavedInstanceState so that no matter what, everytime an activity is started it will appear with any and all data the user has inputed(sp?) thus far, during this instance of the application. I dont want to save data beyond closing the app. I do want the user to be able to navigate back and forth as much as necessary within the activities without losing data until the app closes.
This is what I have:
Activity A: has buttons to: Activity B, C, D, E, and F(F=email intent)
Activity B: has buttons to: Activity G, and A
Activity G: has buttons to: Activity B, and A
Activity C: has buttons to: Activity H, I, and A
Activity H: has buttons to: Activity C, I, and A
Activity I: has buttons to: Activity H, I, and A
Activity D: has button to: Activity A
Activity E: has button to: Activity A
Is there a better way to set up my navigation? (this is optimal for the context I am fairly sure)
And what is the proper launch mode/Flags/savedinstancestate and whatever to achieve proper result? I can explain elaborate as much as needed.
I am aware I need to save the data the problem is setting up the activities so that the navigation buttons i have put in each activity always open activity showing all previously saved data (in the current task instance)
I have all activites set to launchMode:singleTask, I have not manually coded any flagsand I have set up onsavedinstance properly but something as simple as: Activity B-> Activity A-> Activity B (using only my buttons) Activity B saves nothing after going to A and back again (again I am not using back key, I am using my navigation buttons in the layout) How can I fix this??
Oddly tho Activity A DOES retain memory but none of the other activities do
Why you're using singleTask?
Aren't you looking for
intent.setFlag(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
?

Categories

Resources