which is better to execute Database query on MainActivity or SubActivity - android

i currently following tutorial making notepad, this tutorial using 2 activity main activity and edit activity, edit activity is using to fill data for database , but the query is executed in main activity
so the data must sent back OnActivityResult from edit activity to main activity .
the main question is i want to know why we must pass data to main activity instead of execute insert or update query on edit Activity, is this the best way?,
Can anyone explain why?
Thanks

There you have the 2 activities, one (Main) is meant ust to display the notes, the other (Edit) will let the user to create the note. When the user confirms the creation of the new note in the EditActivity, it will finish and in the MainActivity's "onActivityResult" the notes will be loaded and displayed in the list. So if you want to separate the creation from the visualization of the (I highly suggest you to do this), you want this approach.

Related

Updating Listview

I am developing an android application similar to Facebook android application. On the timeline screen I have list of posts , user can like or comment any post. When user like a post, to highlight the like button I took the object from array , change its liked status and notify adapter so like button gets highlighted.
If user clicks on a Post a detail screen opens up. In detail screen user also has option to like the post. Now my problem is, if user like a post from detail screen and went back, he should be at the same position in the list and like button of the post on that position should be highlighted.
I tried to do it by starting detail activity with StartActivityForResult() method and passed the selected object. In onActivityResult() method I am getting back the modified object from detail Activity. I replaced the original post object by the modified object in array and notify the adapter.
I am not sure about my logic that its a good one or not. I need a better solution to do this. I will be very thankful to any good suggestion.
onActivityResult() would be just the right answer but you are not finishing your activity but pressing back/home button instead. So in this case no information is being passed to the parent activity.
A solution that comes to my mind is modifying your objects more permanentle via sharedPreferences or some ORM. But it may be something unpleasant being collecting data from memory in every movement of your app.
What I suggest is transforming your detailActivity to a detailFragment. It would be easier to pass information to the parent activity. In this case the activity that has the full list would start the fragment passing the single element that has been clicked. After that, you would implement a interface to comunicate with the parent activity and in this case the main list would always be updated correctly.
This method requires a very long explanation, too much in fact. I would point out the main steps in order to give a starting point for researching.
Create a fragment similar to your detailActivity. It needs a declaration of the proper interface to communicate with the activity.
Make your activity to implement the interface and override its definition. This function will handle like button being pressed. So is it here where you need to update the main arrayList.
Change one main fragment with detail fragment when an item is clicked.
I hope it helps you to find final solution for your code.
PD: I found this url that may content the whole process, just very well detailed.
Yes. onActivityResult() is a good implementation for activity to activity communication.
Your logic is not wrong.
But if you want it to be simple, you might want to try some third part library like EventBus, it could help you to deliver your message between components like activities easily.
In your case, a StickyEvent is helpful.

How to pass SQL database data from one activity to another?

I have a simple application consisting in a contacts database (using a SQLiteDatabase class).
In my main activity layout, there is a "View Contacts" button. When pressed, I create a new activity (let's call it "ContactsActivity") in order to show a LinearLayout with the contacts data.
My question is: how can I give ContactsActivity access to the database?
Do I pass some data from the main activity (which I think is not an option)?
Do I just kind of open the database from this activity?
Do I use the same method that I used for the main activity (I mean, using a SQLiteOpenHelper and so)?
I don't know if I am giving enough information; otherwise, just ask me and I will provide any necessary info.
Thank you.

Update View with updated data in Android

I am noob in Android. Suppose I have two Activity in my android app.
Activity A and B, currently I am on Activity A then on click on one button I am now Activity B. Here From Activity B I want to update some view with updated data that is in Activity A. I got updated data in Activity B so Should I use here LocalBroadCastReceiver for this or anything ?? so that When I press back then app should show me updated data in Activity A
Should we use our custom callback here to update the UI view data of Activity A from Activity B ??
No, you shouldn't use BroadcastReceiver for that. The method depends on the size of data you want to transfer from Activity B to Activity A. If it's quite small, you can start Activity B with startActivityForResult and then get data back at the onActivityResult method (in Activity B you should use setResult once you are done). If the size of data is quite big, it's better to use DB for storing it instead of keeping it in memory.
okay , there are multiple ways to do it :-
you can use a static variable to store you data that too of application level ( might not be a good approach ) check the value of vairable in onResume() and set it to the view.
you can use sharedpreferences if your data is not that large , store the data in Activity B and fetch it on onResume() method of activity A.
as #nikis has told you
if your data is too large store it in db.
I dont think broadcastreceiver fits right in your scenario !!

relaunch activity stack

i have an application with many Actvities , i need to call each of those activities with paramaters to retrieve data from database or a file , but if i call an activity a second time i dont want that the activity retrieve data again cos it can be boring for users .
Example :
i have main activity with menu that can call 3 activities : A,B and C
each one of them need parameters to access database
in each activity i have a link to navigate between them , i need to
call back the activities then from stack so no need that they access
database again.
Any suggestions are greatly appreciated.
If you are not finishing your activities, data will persist. You dont have to do anything additional.
Otherwise you can extend Application class and save your data into some data structure there, which you can retrieve any time later.

Best Practice activity- switching and view- changing in android

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.

Categories

Resources