The app has a database, but after each update, the new information does not appear in the app, I have to delete the data of the app to see the new info. What can I do? Is there an option to implement a button or something similar? I just want this procedure done within the app, but if there is another way, much better. Is there a way to do it without losing data? Thanks in advance.
If you need to refresh the Activity after for example having updated data in the database, you can update the View of an item (i.e. TextView ...) through invalidate () or if you use a Fragment perform the repalce() of the fragment.
Related
I'm making an app with a lot of user input from edittexts stored in a Room database with an attached TextWatcher. When a fragment is opened, I want to repopulate the EditTexts with the values that have previously been stored. In order to do this, I am currently observing LiveData from the Database once, and removing the observer once the EditText is re-populated.
This works well the first time I open each fragment. The problem that I'm having is that if I make a quick change in one fragment -- deleting the text, for instance -- and navigate away, then back again, the EditText re-populates with old data instead of the newest change.
I know from the Database Inspector that the new value is being stored, but when the EditTexts are re-populated, the TextWatcher causes the old value to be stored in the database again, overwriting the new value. So I'm guessing that the issue is a delay in the Database updating the LiveData, and the EditTexts being re-populated before the data is actually returned from the Database.
Am I going about this in the wrong way? Should I be using SavedInstanceState instead, and somehow only using LiveData on the first load? I'm not currently using a repository -- I'm teaching myself Kotlin and haven't learned about them yet. Would that solve this issue? I have a lot of fragments and a lot of EditTexts, so I'm trying to find the cleanest way to deal with this.
I hope this is clear. Thanks for taking the time to answer!
I want to refresh my HomeScreenActivity so you can see your balance when you have paid (it works with NFC/HCE).
When you scanned your phone you go back to the HomeScreenActivity. The problem is that when you go back, your balance button is not updated so you have your old balance. You get your new balance when you go back to the login activity and then login again.
I tried a lot to fix this but nothing seems to work. Maybe the problem is that the communication with your balance goes from a database/API.
I hope someone can explain me or fix the problem!!
P.S. I do not have a adapter for balance!
Please use onActivityResult method for the actvity where you want to refresh the amount. And in onActivityResult method reset the adapter and call notifyDataSetChanged().
You have many way to do this. You can research about onActivity result, it will help you solve this problem. EventBus, observer is the same solution.
You have a lot of ways to make your data always up-to-date:
I will list some of them:
Use Realm for your local DataBase. It have own classes extending recyclerviewAdapter and other to notify you when data in tables was changed.
Use new Android Room library it also can do the same thing using LiveData
Use RxJava to subscribe to some changes in you app. When changes will happen Rx will emit new item to all subscribers.
User Observer OOP pattern
NotifyDataSetChanged doing the same thing. When you expect your container data to be changed you call this method and RecyclerView.Adapter will rebind all existed ViewHolders to new data.
These are not all possible variants to organize notifications. You can also use EventBus or kind of timer to refresh data with specified delay(this isn't the best one).
I have a problem as, I am creating an app as a social networking app in which there are posts from users, these posts are shown in a listview but the posts are updated only when we recall the same activity because parsing is mandatory for the same. I want that the listview should be automatically refreshed when we pull down the same as iPhone Pull Down to Refresh functionality. I don't know how to implement the same. Please suggest me for the right solution.
Thanks in advance.
You can find example same for Pull to refresh over here.
https://github.com/guillep/PullToRefresh/downloads
https://github.com/johannilsson/android-pulltorefresh/downloads
You must be having a background Thread /Service that fetches posts from the internet.You can implement a custom Observe-Notify event pattern. Service/Thread will implement custom notifier, Activity will implement custom Listener.When activity starts, it will register itself to Notifier, When posts arrive, Service will call notify and pass the posts object,Activity will get notified , it will update the adapter for list and call notifyDataSetChanged() method on the list adapter.
I am still searching for the best solution howto use a layout with a menu and a toolbar and inflate or start activities in android. My question may sound confusing, but im trying to explain it in an example.
Lets say im programming an android app (surprise.. i really do)
My app can do following:
User can log in [3] or register [2]. If he logs in, a new activity starts and his dashboard will be shown. If he registers: an activity for the registrationprocess starts.
Registrationprocess: user puts in his desired username and password and presses a button to accept. His data will be formvalidated and if valid, a new activity starts where he can choose his settings. Backbutton works and data can be passed to the new activity. After the last registrationwindow data will be saved and dashboard started. Starting new Activities is fun!
Now THATS where it gets complicated. Dasboard has an 'actionbar'(top) and a 'toolbar' (bottom, like tabs). So everything should be viewed in the middle part of the viewport(from now called main view). No more activity switching :(, tho.
Currently each tabclick removes all views from the main view and adds its new view. Look great, can be animated and works like a charm. Except: its currently not dynamic.
So... i don't know how to solve it the best way. For example: i fetch data from a webservice, create a listview out of it and it's extending listactivity. This activity i can't start but this data need to be put into the main view. How can i do it the best way?
And is it efficient?
I'm practicing and it's actually my first small discussion i want to start. So... FIGHT! ;)
UPDATE:
I've seen an interesting way to start activities and get results.
Launching activity through intents
. Is it possible to insert new/ update views after activity started? I would then generate my results in a separate activity. Update the view. Return back to 'dashboard' and load the view that was just updated. Possible? Or inefficient? And how can i update a view out of another activity? There is so much i need to learn :/
UPDATE2:
A good example of an app that has done it: Google+
Too bad i don't have their sourcecode ;)
UPDATE3:
What is best?
load a new activity, disable animation and set selected toolbox tab +
disable backbutton functionality
startActionForResults, fetch results and update current view (still don't really know how that would be possible)
viewFlipper onflip changing+updating data in flipped view.
I still don't know any efficient solution. Or am i missing something essential? I've just finished my ListActivity to fetch data from my webservice. But it still runs in a separate activity. How can i implement it into my "main view" now? Ofcourse... i could set a list my custom adapter. But currently im updating and fetching data from the server when i create the listactivity.
Im afraid this could be the only answer i'll get: Embed external Intent in main Activity
UPDATE4: I'm trying something.
Based on nininho's answer (thank you!) im trying the following approach:
Start Dashboardactivity and create a ViewFlipper.
Each Toolbarclick represents a certain ViewFlipper page.
Each Page has a Listadapter implemented and shows different results (different webservice queries). (ListView, GridView, with profileimage, without profileimage)
On Toolbarclick start AsyncTask or Service and notify List in current Page that data has changed. (ofcourse IF data has changed). Switch to page that was clicked.
Implement updatefeature. On scroll to bottom of list = fetch more data and add it. Update other lists automatically after 5min. or update list on update-button click.
PROs so far: Backbutton standalone for whole activity. Page-flip-animation possible. Async updating of lists and still possible to switch to another list.
CONs: ... someone has any? What about efficiency of such an approach? Does the ViewFlipper carry all the information so the performance would go down or does the viewflipper recycle its Views (like ListView)?
UPDATE5:
If i have some time i will make everything here more read- and discussable. Don't be mad at me for reading my rubbish ;)
From what I understand you want your app to start, fetch some data from the internet and after show this data on the main screen.
I don't see the need of a second activity to fetch the data because from your explanation you want to use it only to fetch the data, so the best approach would be:
Create one Activity (your dashboard)
Start an AsyncTask or Service on the background to fetch the data.
When the fetch ends, notify the activity that it ended.
Change your dashboard to show the list (you can use a ViewSwitcher if you want some animation or just create a layout with the list invisible and then change to visible).
ps: you can use a ListView outside of a ListActivity, just create a ListAdapter to create the ListView items and add this as the adapter for the ListView.
I am currently working on an app which takes a number of user entries. I want to have each EditText field on its own page, and instead of having a seperate activity for each entry, I wanted instead to call the same activity again. Is this possible, and if so, is it a feasible solution? Thanks.
It is possible but I don't think it is the way to go. Basically if the next input is a separate action then it deserves its own activity.
That is the way you are supposed to do it.
You could store the gathered values either in the Application class as a temporary storage or you can save it using SharedPreference. However if it is only temporary data I advice you to use the Application class rather than writing it to a file.
I would think that if your UI doesn't change (significantly) between views, then reusing your activity and displaying different data seems fine to me (I do this myself).
I keep an object on the Application class that contains a list of the sub-objects (Inputs in your case).
On the top level object, I keep the index of the current index.
This works very well, does not leak memory and is very fast to render as I swipe through my pages.