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.
Related
For my current project, I will be using this SlidingUpPanel library.
The sliding up panel will host a fragment that will contain application/global level information. This panel will be in every activity in my application. This will be very similar to the way Google Play music works.
My question is, what is the best way to persist the sliding up panel fragment throughout my application? I see myself going about this in two ways...
Storing the fragment inside the Application class and loading it at the start of every activity (somehow, not even sure if this is a possibility).
Storing the data that it will display in the Application class & loading a new instance of the fragment, passing in the persisted data.
Which one of these two ways would be the best? Is 1. even possible? Are there any coding landmines with these approaches?
Storing a fragment to persist throughout the application lifecycle would be pretty unorthodox, mainly because the fragment class should be able to follow it's normal lifecycle events (in this case, mainly onPause and onResume) instead of being stuck somewhere in the application class.
It is definitely common practice to store the data and load it each time you display the fragment. If you want to enable some sort of cacheing or singleton pattern to access the data, it should most likely be with another object that the fragment can access but is not a member within the fragment.
There is a good video from google about leaking views and it touches briefly on the pitfalls of doing some similar to what you're proposing in bullet #1.
I think the structure of your app looks like it should be a single activity where that bar is in it, then the main content is a Fragment that you replace and use addToBackStack on in order to maintain the use of the back button. Otherwise, you are going to have a lot of repeated code with solution 2 (which means a lot of repeated work in the case of bugs etc., not very maintainable), or leak views using solution 1.
More info on providing a proper back implementation
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...
Is it possible to add a textView on Activity B without showing it?
What I mean is, when i press a button (on DiallerActivity), then a textView will be added on HistoryActivity without leaving DiallerActivity.
How can I do this?
is it possible to a add textView on Activity B without show it ?
No, not directly.
An Activity is a special case Android class and shouldn't be treated as a normal Java class. Effectively the purpose of an Activity is to act as a framework for a UI so, if an Activity isn't visible then there basically is no UI. In other words, how can something be a user-interface if the user isn't able to see or interact with it?
As logical Chimp suggests, the changes should be with respect to some form of data history (a database or SharedPreferences) and it is the responsibility of your HistoryActivity to update its visual elements (TextViews) next time it is started.
One Activity shouldn't try to modify the look, feel or behaviour of another Activity except indirectly by changing some form of global data or state or by passing data to it if the first Activity is responsible for starting the second.
hmmm - not sure I fully understand your question. Are you wanting to append the dialled number to the 'HistoryActivity' without showing it?
If so, I suggest you separate your data (list of dialled numbers) from the view (HistoryActivity). Then, you only have to add the dialled number to the data record used to hold them (suggest a list). When the user opens the HistoryActivity, then you can just draw as many text fields as required to display the data.
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 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).