Anyway to append to the activity stack without doing a StartActivity? - android

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.

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.

Android/SQLite - "Managing Database in one place statically" vs "serialisation and intent method"

I have one general question:
what do you think about following two alternatives considering style and speed
having a cached list in the main application and always using this list and load data on demand (and remove it on demand, for example if the cache get's to big)
always serialise the data, adding it to the intent, give it to the sub activity, working on the data, serialise it again and give it back to the parent activity and replace the original data in the list with the one gotten back from the sub activity
in my case, i've probably always a list with a thousand entries... and about 5 levels into the deep... always serialising costs time... and i've to handle the changes, because the parent activity newer knows, if the data was changed in the sub activity...
the global data cache has the advantage, that I always and everywhere interact with the same objects and never have to take care, that if data is changed, that I update this data somewhere else...
Is that a bad idea? if so, why? I want to speed up my application and am thinking about changing it to this model...

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.

includeed layouts - update in multiple activities

I have an app with multiple activities and multiple layouts. However, one piece of layout is included on several activities. I also have a thread which updates this layout. However, when i switch activity it doesn't work. Since the layout is included the elements have the same ID's, shouldn't it just work? Or do I really need to fetch an object for each element in the layout and feed it into my thread in order to make it update the elements in a new activity?
You should run the update code for each Activity/View, although the XML included is the same, each is a different instance.
My suggestion is on Restart verify is there is any modification to do in each activity, a simple way is to each Activity extend a BaseActivity that has this code.
I include a layout for adverts in my app, but on each activity that uses it, the adverts need to be reloaded.
If I call an activity from one that is using the same included layout when I go back to the previous activity it's still there.
I guess this is what you are seeing....
So you can also save that data inside sharedPreferences (if it is little data and primitive objets or parceable objects).
Also you can extend the Application class and store the data there and update every activity inside the onResume() method. that i believe is the best way to handle this. and this is quite simple to do.
Ask google about extending the application class and he will provide tons of results on how to do it. its an easy way to pass data between activities and/or keep a reference to a single object which you will use throughout the app. Just be carefull to clear it when you wont need it anymore because it will stay in existance untill the application is finished() (which comes with the application extension living thru the whole application lifetime).

Can I use Intent to launch the same Activity (Android)?

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.

Categories

Resources