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.
Related
I have a question around ListActivities, put into an example for hopefully more clarity. ;)
My application has a TitleListActivity which shows the published titles of a particular author. This list might contain one or more titles depending on the author. When there is only one title in the list i want to immediately start the TitleViewActivity for the particular title rather than showing the boring list with one title. I assume pretty common thing, just havent found any explanation on it so far.
Easy approach would be to check before calling the TitleListActivity how many titles there are for this author and start the respective activity. As the TitleListActivity call can happen from different parts of the application I would like to centralize the logic which decides what Activity to call.
My question is what is the best practice to achieve this. Can this logic be added to the TitleListActivity in my example efficiently. All the history back button logic etc should work of course.
Any suggestion highly appreciated
Thanks
martin
I would probably make a Activity launcher class/controller with a static method for launching the TitleList or Title. Would be something like ActivityLauncher.lauchAuthorActivity(context, auther) and that function would decide on what activity to launch.
Better use Intent.putExtra() instead of static method or class.
You must have already been using something like a AuthorId to get respective author's Titles.
Just use 1 more attribute TitleCount
intent.putExtra("TitleCount", n);
check it in TitleListActivity, and if its 1 finish() TitleListActivity in the 1st line of onCreate and open the Title Page.
If you are getting all the data of Titles(including the Title count) in the TitleListActivity check the Array/ArrayList size which has these titles.
I'm doing my first app, a RSS Feed application, for learning the multiple technologies associated, like xml, parsing, connecting to the internet, getting the information, processing it, etc.
I've decided to use the newest Android elements, such as the Action Bar and Fragments. So I've done an action bar with a few options, like Refresh (which refreshes the RSS list), Preferences, About and Exit. The main issue is with the Refresh.
I'm pressing Refresh and the option creates an object which will get the XML, which should return the information for the newsList Fragment. But I can't pass the information to the fragment, but I also can't Toast the xml information to the screen, so I cannot test if I'm getting everything correctly.
My programming background is not in Java, I'm used to Web Developing (PHP) and scripting (Shell) so I guess I'm missing some basic stuff, which I apologize in advance.
Can anybody at least give me some hints, in order for me to know what to search and get back in the right path?
Thanks a lot!
So you have the class that handles the downloading/parsing of the RSS feed, but you don't know how to pass the processed feed-info back to the ListFragment right?
There're multiple ways to solve this. You could use the callback pattern for your rss-retrieving class for example. So the Activity starts off this rss-retrieving object asynchronously, and registers itself as a listener of this object. When the rss-stuff is done, the object notifies your Activity about this through the listener-interface.
In response, the Activity can get a reference for the ListFragment (using the FragmentManager) and call a method on it that refreshes the list, passing in the parsed RSS-info as a parameter.
In my android application I have this situation:
I have a single activity across all my applicaton, say MyActivity, it contains a listview and custom views inside it. The activity has a url property so that different data loaded for each case. The problem is I can't use android's built-in stack mechanism to handle navigation. For example when I try CLEAR_TOP flag it doesn't help me, I think it merges all my stack into a single activity. I think I'm going from A to B but android thinks I'm going from A to A, I don't know if this is right but seems so. I need something to handle the navigation. anybody making application with a single activity can help me better. My application is something like a web browser, thanks.
just remove clear_top. when you try to launch MyActivity from itself, android will treat it as a separate activity, thus adding a new item to the stack.
You may simulate the back stack by placing some data (let say your URLs) which will help you distinguish your different internal "activities" into a desired collection (i.e. LinkedList).
You will then need to override onBackPressed() method in your activity where you should poll/peek the element from your stack and refresh UI. Correspondingly you will need to push an element into your stack when you retrieve an URL and also refresh UI.
I'm parsing a huge xml to display a list of titles in a listview in activity A. However the same xml also has details for a list item which needs to be shown in a different view (like list mail subjects/view mail details scenario).
On click event of this list i dont want to load a new activity with a bundle, parse the same xml and show detailed view, while i have the required data in activity A itself.
I figured out a way to hide show layouts in my XML to do this as required, but handling back button is an issue. I can probably do this by capturing back button action, but want to know whether there is a better solution for this.
Like broadcasting an intent to A (from A itself) and somehow managing to add that to the activity stack.
Excuse if there is a duplicate question, couldnt find one when i searched.
BTW, i dont want to do a solution with a database caching.
I would handling the back press. Just use a flag within your activity that tells you in which view you are (so back within the detailed view shows you the overview view).
Another way would be to save the values in your applicationContext. Much easier way to do it than database usage.
Take a look at an answer here: How to declare global variables in Android?
But I would definitely go with handling back presses. I have a solution similar to this where I use the same listview in the layout and instead I use different adapters depending on which detailed view the user is in.
Handling back press is the easiest way to go.
Else you could also pass the information to view as Intent extra to the second activity.
Another possibility is to have a local service running in the background and in charge of loading your XML and offering access to its information in a convenient way.
You can also stuff the XML content in an Application object of your own. However I have had not so great experience with that option in some projects.
I would use a second activity. Pass additional data (like contact list, message details, etc.) to it and display it. How you keep parsed XML in memory is up to you to decide (static member? yuck! but it works).
Now back to original Activity. Does your source XML change a lot? Maybe you can parse it and put all data into a DB so that you could retrieve necessary (and hierarchical) data quicker. This way you do not need to deal with storing lots of data in memory, re-parsing and you could perform search faster.
On click event of this list i dont want to load a new activity with a bundle, parse the same xml and show detailed view, while i have the required data in activity A itself.
Cache the parsed XML in a static data member. Your activities that need the data look at the static data member first, then kick off the parsing if and only if that cache is not there.
IOW, this is not an activity problem, but a data model problem. Do a better job with your data model, and your activities can behave naturally.
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.