Passing Data to an Activity without Starting the Receiving Activity (Android) - java

I am making a contacts app that presents the user with the following Buttons, "Create New Contact", "Edit Contact", "Delete Contact", "Display Contact" and "Finish".
There is a Contacts List activity which must be entered/displayed when clicking on Edit, Delete, or Display. When creating a contact in the "Create New Contact" Activity, I would like to send the Contact object (it implements Parcelable) to an ArrayList within the Contacts List Activity WITHOUT starting it, once the Contact is added, I would like to return to MainActivity.
I am unsure how to go about this as using Intents results in going straight to the activity and I am also unsure how to cause the ArrayList within the Contacts List Activity to save.
Any suggestions? I do not need exact code but I am unsure what direction to go in to accomplish this task.

In my opinion the data model should live in the context of the app and not in the context of an activity. Then the data is accessible from all activities. Now the only object you need to pass by intent is the id of a data set.
This general principle also enables what you want to do, start a new activity, do some data manipulation and send a new intend to the next activity without displaying the previous one at all.

There is a logical problem: if there is not a Activity instance, why do you need to send data to it(even not existed)?
Alternatively, if you'd like the data existed within the whole user lifecycle, it needs to save edited data(ADD,EDIT,REMOVE) in somewhere like local storage or Database. And fetch data(then parse it to specific object) from persistent layer when start the presenter activity (where received data and presented them to user).

Related

Waiting for database information before displaying list

I have a dropdown list in a fragment that I access by referencing a ViewModel I initialize in the main activity, said ViewModel retrieving data from a mySQL database using Volley. This normally works as almost always the data has been successfully retrieved by the time the user opens the fragment requiring it, however, if I update the database and refresh my ViewModel's contents this is almost never the case (upon update I immediately return to the fragment where I need the data)
what I'd like is to be able to show a loading prompt until confirmation of successful retrieval of the data, but all resources I can find on this use the now deprecated AsyncTask, so I'm not sure how best to approach this.

How to maintain state of object throughout the app and change the UI at the same time

I'm developing an app having HomeFragment which displays posts of the user you are following. Now each post is having Like and Comment option and when you click on user profile pic or name, it will redirect you to his/her profile which again displays all his/her posts. Now again if you click on one of the post it will redirect you to post detail screen which implements ViewPager where you can change the post by swapping left or right.
My question is every post has Like and Comment option with Like and Comment count, if you like any one post then it should reflect at all the activities(Home, User profile and post detail).
Currently I'm handling all these using startActivityForResult and onActivityResult (eg: if you like post at position 2 and when you click back button then I'm sending that position with new count and notifying that particular position with new like count at HomeFragment) which is very confusing and I think is not the proper solution.
Is there any Design Pattern or any other mechanism to handle this?
If you didn't get my question then let me know, I'll elaborate it more.
Thank you.
My approach in my apps:
In activity onCreate I start Intent service to load data from server. In Intent service data are loaded from server, then parsed and pushed to local database (I use super fast Realm database). When intent service finished, UI is notified to refresh it's content. When you return to previous activity onResume is called where I reload data from database and refresh UI.
To sum it up, UI works only with local database where all data are stored. When new data are loaded on background thread, this data are also pushed to local database. So no need to send loaded data (only needed object ID's) between activities because database contains always up to date data.
You can create singltone class which will hold every posts likes and comments.
Every time you open activity/fragment you pull data from this class.
Every time user adds like or comment you push data to this class.
I also recomend you to take a look at MVP pattern

Android: Structure of alarm app

I am creating an alarm application. I currently have 2 activities, one called AddAlarm which creates a new alarm and allows users to enter data such as alarm time and title. The other activity is called Alarm and it displays all the alarms that are currently set in a ListView.
What I do not understand is:
1) How can I pass the data from the AddAlarm activity to a ListView item in the Alarm Activity?
2) How can I pass the data back to the AddAlarm activity when the user edits the alarm?
3) How can I store the data each alarm when the app restarts?
I have already tried to create a bundle for each alarm, however, I learned that these cannot be stored in SharedPreferences and I am not sure how bundles can be used in an arraylist.
Do you guys have any suggestions? Thanks a lot!
From your question, what is clearly understandable is that you are facing problems with data storage for your app.
You do not need to pass data from one Activity to the other, what you need to do is use a persistent storage mechanism, such as SQLite or Realm.
The idea is simple, I will list down some of the important points.
When the user adds new alarms from the AddAlarm activity, just store the data in the database
In the AlarmActivity with the ListView, make a query to find all the alarms which have not expired yet and show them
Whenever an alarm expires, make the necessary modifications to that particular alarm entry in the database so that it is not shown again
This would be an idea way to handle the scenario for your app. Please have a good read at this.

Storing data across Fragment and Activity life cycles in android

I need to store an array of Custom Objects to populate a ListView in android. I fetch the data to populate the ListView in a Fragment that is a part of NavigationDrawer (The Fragments are replaced frequently). The next time I start the Fragment, data is fetched again. I want this data to be persistent even when I go to another Activity and come back.
I'm not considering using a database as I don't need the data to be restored when the user restarts the app. I just need the data as long as the user is actively using the app.
I've considered the following methods:
Singleton class
Subclassing the Application class
Saving the Instance state in the Fragment/Activity
Is it fine to store an array of 2000 objects in the Application subclass? Is there a better methos to do this?
You shouldn't consider using the Singleton Class, if you want your data to be persistent.The best Android practice to store persistent data are :
1.Application Preferences
2.Files
3.contentProviders
4.SQLite DB
You can read more about it here : http://developer.android.com/guide/faq/framework.html

Android and SQLite interactions: updating row with multiple intents

I have a program with a main activity which spawns 4 different intents with lists in each. There is a database used to track all of the choices from these lists. Currently with the way that startActivityForResult() works I am having the program send back a comma delimited string which is saved in an array in the main activity and upon main 'submit' it is all saved to the database. The lists represent choices made on a per-day basis but each list corresponds to the same day. I am using putExtra() to send the correct list to the activity.
This can be an issue if the user closes the program or android kills the app the array may be lost. I want to update the current dates data from each spawned intent but when I would do this it would create a new entry.
Main question: Can I set a global date value (that the user sets in main) that each intent can reference to use update for my sql statements. I want to make sure that there is no loss in data. Currently writing to the database is only done in the main activity upon submit but I would like to have the app write to the db from each intent.
This question is very abstract in my brain and if there is a need to clarify I will do my best. (I am at school and just brain storming things to try when I get home)
Write the array to Shared Preferences in onPause() and onTerminate(). You can use the array element number as the key and whatever is in that array element as the value.
I can't quite work out where you're going with the global date/time stamp, but if the point is to prevent data loss, Shared Prefs can be damned handy.

Categories

Resources