I want to use a ShareActionProvider to provide a button/submenu in my ActionBar to share a file that is generated from by my app's database. The file that will be generated depends on which tab the user is currently viewing. Since the data in the database may change, the file cannot be generated at the time the Activity is created, but must be created when the user requests to send it.
To this end, I have been following the official Android guide. Unfortunately, according to the guide,
[...] if the action provider provides a submenu of actions, then your
activity does not receive a call to onOptionsItemSelected() when the
user opens the list or selects one of the submenu items.
There therefore appears to be no event that I can listen for to call setShareIntent() at the correct moment:
onOptionsItemSelected() is not fired, unless the ShareActionProvider button is consigned to the overflow menu, which defeats the point of using it at all
onMenuItemClick() is not fired, for unclear reasons
onShareTargetSelected() cannot be used to modify the intent. Setting a new intent only applies to the next time the item is selected.
It seems that a key event is not covered by the API. How can I achieve the functionality I need?
The file that will be generated depends on which tab the user is currently viewing
Update the Intent when the user changes tabs.
Since the data in the database may change, the file cannot be generated at the time the Activity is created, but must be created when the user requests to send it.
If this is truly a file (EXTRA_STREAM), use a Uri pointing to a ContentProvider of yours that will generate the file on demand.
If by "file" you mean just adjusting EXTRA_TEXT, do that as the user changes the data (e.g., TextWatcher on EditText).
Though I agree that ShareActionProvider ideally would have a "dynamic" mode where, when the user taps it, it asks you for the Intent at that point. I haven't seen any way to do that.
Related
Hi I have problem with planning implementation of specific search interface.
I am making navigation application where you can navigate from point to point.
For this I need two seachviews both with different searchable configuration, where I can define different custom intent action to recognize if user enter source or destination and react properly. I have already created suggestion logic because I am using one SearchView in other activity and already have different configuration there.
So my plan is creating an activity for result which will be in same time SearchActivity where I will setup two different SearchViews components where based on user input there will appear suggestions and after clicking them (first later second) the intent will be picked by CUSTOM1/2 ation intent in onNewIntent() as it will be "singleTop" activity and stored properly. Then i will process stored source&destination data and finish with proper response code to the source activity with extras based on processed data.
I don't know if I explained it properly. The main problem for me here is that I need to have two different SeachViews widgets. How to achieve this?
Eventually I can manage both SearchView suggestion results with default ACTION_VIEW and recognize them by sent data but I think the first plan is more clear.
PS I am planning to put them directly to the layout without appbar.
I have 2 UI Activities (w/ Fragments) as well as a SettingsActivity (derives from PreferenceActivity) in my first-ever android app. It 'works' but I want to make sure I'm doing the idiomatic right thing as the user flows between views and the preferences.
Specifically - when I change my 'location' preference (when the user is in the SettingsActivity) to a different zip code, I would like the app to fetch some background data from a service. But then I need to refresh the UI in whichever View preceded the settings change (taking the new data into account).
So is it 'the right thing to do' - to do work (fetch the background data) in the PreferenceActivity's onPreferenceChangeListener() when the zip code change, and then have the preceding UI activity consume the data (that's now updated in a singleton abstraction of the data source) in its onStart() method when the user returns to it? Or is there a better way (e.g., to somehow capture the 'preference changed' in the UI Activities, and do all the work there, rather than in the PreferenceActivity).
Hope this isn't too abstract, I'd share code, but there's so much cleanup I'd have to do that I'm hoping this to suffice.
Thanks in advance.
I would put the functionality into a seperate class MyCurrentZipcode that
can be used by every activity that consumes the zip-code dependendant data and
gets informed, if the zipcode changes.
does the background download of the zip data.
You can create a method MyCurrentZipcode.addOnZipcodeChanged() where the consuming activities can register to get informed about changes.
I'm extremely new to Android Development and I'm decent at Java. I'm working currently on making my first app and I'm stuck at a spot where I've researched for the past two days but I just don't know what to do. I want to make something similar to how Contacts is set up in Android.
I was able to create two buttons in the main activity page and have an intent to another activity by the click of the button(s). But when I come to the 2nd activity, I want the user to be able to "Add Class" and I want to take this to a new activity where the user inputs information such as "Class Name" "Assignment Type" "Assignment Worth" etc. and then hit save. And by doing so, I want this New Class added to the activity with the listview item "Add Class".
But then I want the user to be able to click the new item created, for example, "Math" and when he/she presses it, it opens a new activity with the information stored previously. And I was wondering how I go about that.
Thank You.
You will want to save the user's input data to persistent storage, eg: shared preferences, sqlite data base or plain text file, I would go with a sqlite data base as it's the most suited for this job. After the user saves his data he'll return to the first activity in which you pull all data from the sqlite data base and load it in the listview via CursorLoader and LoaderManager, as this will load the data into the listview asynchronously.
I don't know your level of skill but I suggest start to learn about android's sqlite, good tutorials can be found on youtube, like this one and for CursorLoaders this one.
First some background.
In my edit item activity I have added search functionality to change of one of the item's data fields. (Its manufacturer and make). Since the user can select from quite a large amount of items, we have decided to use search with suggestions to change the manufacturer/model.
Using the normal Android search, from the edit activity I can override the onSearchRequested() method and add the current item's ID as part of the extra data. The actual updating of the data item, is done in the search activity. (I know, not the best idea, but my edit activity doesn't know what the user did in the search activity.)
This works fine for a simple search, but I can't seem to find a place to inject this context data (the item's id) for Search Suggestions.
I have read through the android docs and the closest I have come across is SUGGEST_COLUMN_INTENT_EXTRA_DATA column for the resultant row in my suggestion, but since my search content provider also doesn't have any context of what item I am editing, it doesn't seem like it will solve my problem. The Intent is still launched from the suggestion by the Android system, and I can't seem to get the required context info added to it.
Is this even possible given that search suggestions seems to be geared towards context-less global searches i.e. Android's quick search?
For completeness sake: we are targeting platforms less than Android 3.0, so the functionality of SearchView is not available.
Good news: the Intent that the suggestions launch directly is the same one that the activity sets up in the call to 'onSearchRequested()'. Any context info can be set there. Can anyone find a reference to this in the docs?
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.