Saving value from 2nd activity to 1st without losing the updated data - java

This app has 2 activities. The main activity shows 4 images as image buttons. When user gets into the form page, user can edit the information of the images. However the info does not save when I edit another image and then return to the first image.
Any help to this would be very appreciated !

If you just want to retrieve an updated data from an activity than easiest solution is to create any class to save constants and update that constants while editing your data and when you want an updated data just call that constant variables from that class.

Related

Sending Images Between Activities - Android (Kotlin/Java)

I have a class which encapsulates the information I want to pass over and it is parcelable. So I can pass its properties from one activity to another by using Intent.putExtra() method. But I cannot send images with it since images' sizes are larger than it should be (it doesn't matter whether they are Bitmap or ByteArray).
User downloads images from Firebase Storage and it is very likely that when user views an image, she won't see it again. So saving images in a cache-like storage isn't really necessary.
In Swift, I can do something like this:
// We are in currentViewController which is the activity that will send the image
let someNewViewController = NewViewController() // Create an instance of NewViewController class which is the activity that will receive image
someNewViewController.image = someImage // NewViewController class has a property called image, assign it an image from currentViewController
navigationController.pushViewController(someNewViewController, animated: True) // Go to someNewViewController activity
And then you can easily use image in the someNewViewController by accessing image property. When someNewViewController deallocated, image will be removed by garbage collection.
Is there a similar way in Kotlin/Java? Or is there an alternative to putExtra() that will allow large files to be transferred?
Thanks in advance!

how to get data from one activty to another several times Android

i tried to make an basketball game with 2 activities.
one is the opening screen with a play button and when i press on play i build a pop up window that will give the user some inputs like player name , percentage form field and more some data. how can i do that after the user enter all the details at the pop up screen and press enter it will send the data to the arraylist in the MainActivity and its will pop up again (i need that the pop up will open 6 times - each time for each player. 6 players at all). can someone plz tell me because intent is not good here from the pop up. thanks
You will need to save the data somewhere preferably , you can use an SqLiteDatabase which saves the player information having playerId as a Primary field.
I think no need to save all data in the database or shared preferences!
You can use a static class to store data or use EventBus to interact with your classes.

Load state of particular recyclerview item

so I have recyclerview populated with data from database. Each row contains two buttons one for play and second for stop time. So what I'm trying is to save state of buttons inside recyclerview. And I used
PreferenceManager.getDefaultSharedPreferences(context).edit().
putInt(Constants.numberOfbutton, getAdapterPosition()).apply();
to save particular position. I have inserted that line of code inside button click and I checked it's saving correct position. What I'm trying to achieve is, when user enters the app after closing if he lefts any of the buttons in play mode to restore that state and continue timer. But problem is when user enters the app all buttons are running. I'm using chronometer for presentation of the time. I think here is the problem in this method:
private void startTime(int position ) {
Data data = datas.get(position);
chronometer.setBase(SystemClock.elapsedRealtime() + Long.parseLong(data.getTime()));
chronometer.start();
}
UPDATE EXPLANATION:
So I can load from database for each item of recyclerview it's time, date, name etc. But I can't figure it out how to play chronometer of just particular item when user enters the app again. NOTE AGAIN: I'm performing correct saving getAdapterAtPosition and loading data from PreferenceManager. I've checked that. Even I tried putting the number of one of rows but same issue appears.
Any help very appreciated. Thanks.
I have found the answer. I save the boolean in database actually I used 1 or 0 than parse that to true or false acordingly is button play pressed or not. Thank you all for your comments and trying to help.
Try to use handler or alarm service instead of chronometer

Android: How can I exchange data between multiple still running activities directly?

I need a nice way to exchange data between two activities directly. I have one same custom title for all my running activities and in my first activity I display the GPS state in that title with an image (found/still searching). The LocationListener is in my first activity and if the GPS state changes, I would like to update all the titles of my running/displayed activities. At the moment I can only change the title of my first activity.
I know that I can exchange the data through the SharedPreferences and by Intents which passes the data as Extras but as far as I know, I can only receive the extras in the onCreate Methods of my other activities.
What I want do have is, that the data is updated on a still running activity (onCreate is passed).
I hope you understand what I mean :)
Example scenario:
I start up my App. The GPS localisation is running (first Activity). Meanwhile I navigate to another activity of my App. Now, If the GPS state changes I need to update the title of my second activity which is shown at the moment.
Is there any way to solve this problem?
Thanks
Apart from creating an activity with two fragments, there is a tricky way that serves for other scenarios :
Let's say, if you have an variable (let the text in the title bar is an variable) that needs to keep update, while it's impossible to use broadcast (message missing is not allowed or activity is killed), you can use the text saving from shared preference, and extract it every single time.
For example, Activity A uses String from sharepreference, key is "title", and default value is your app name. After B, C or D updates the title text, A also needs to update. So you just update the value of key "title" in share preference. After A is revoked, it will grasp the title in sharedPreference again and update the title accordingly.
Hope this help. :-)

Android loading images to ListView AsyncTask

This is more a question of application logic and performance.
Project Setup: I have an Android ListView which inflates a layout that is more aesthetically pleasing than just simple text. In each row (7 to be exact), I have an Image that is fetched by URL.
The problem: This ListView and its data are set by an ArrayList of model objects that serve as temporary data holders. When the rows are added the model objects are then used to set the data in the specific row's UI. The issue then comes along, that when a user scrolls down on the list a row that was once viewable is now out of the user's view. When the user scrolls back up to that row, the whole process of fetching the Image then happens again.
This seems like it can be avoided by instead of passing URLs through the models then fetching the images, I should fetch the images first, then pass the bitmaps to the model, therefore when the row is visible again to the user, it does not have to re-load the image.
Is there a way to write an AsyncTask that can load 7 images successfully, or do I have to create an AsyncTask object for each image?
Everytime I go this route the application crashes...
You can use Picasso library for this task.
Visit http://square.github.io/picasso/
In the adapter of your ListView, in the method getView, you can put this:
Picasso.with(context)
.load(url)
.placeholder(R.drawable.ic_launcher)
.error(R.drawable.ic_error)
.resizeDimen(R.dimen.list_detail_image_size,R.dimen.list_detail_image_size) .centerInside()
.into(imgView);
You dont need a AsyncTask because this library already implements itself.

Categories