What's the difference between ACTION_EDIT and ACTION_VIEW? - android

They all transfer info in data and seem like when I start a new intent with them all start a new activity. So I don't really know the difference between them.

When starting a new activity using an intent, you could choose to view some data or to edit it depending on your needs by specifying one of ACTION_EDIT or ACTION_VIEW respectively. It may be the case that both of these intents start the same activity and so the result is the same whatever you choose or it might be that they are mapped to different activities and that ACTION_EDIT will start an activity where the data is editable and ACTION_VIEW will start an activity where it is not. As the name suggests, you should choose the action that best matches your intent. If you are specifying a filter for your activity, you should keep this in mind too and filter the actions that your activity is designed to fulfill.
EDIT: Data refers to what you intend to act on. Normally, an Intent contains two things: an action and the data you want to perform the action on. Here are the brief descriptions from the javadoc (which is very good, I suggest you read the section on Intent Resolution):
action -- The general action to be performed, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.
data -- The data to operate on, such as a person record in the contacts database, expressed as a Uri.
Using that information, Android will attempt to find the most suitable activity to service your intent (one with a filter that matches your intent). Activities in your application take precedence over those outside it, but is is possible and common to invoke an activity in a 3rd party app.

Related

Two SearchView components with different searchable configuration

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.

Constructing an on-the-fly Intent for a ShareActionProvider

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.

How do I check whether an Intent is explicit or implicit?

Is there a simple way to find out whether an Intent is explicit or implicit?
I'm trying to implement a navigation drawer with the behaviour recommended in the Android docs, i.e. that the drawer should open automatically when the app starts, until the user has opened it manually.
However, the activities that use the navigation drawer can be started from a variety of places within my app, as well as outside it. I want to open the drawer only when the activities are launched from outside my app, but I can't find a simple way to find out where an intent came from.
There are a few methods in Intent that initially looked promising:
getComponent() would seem to give you the information (null/not null) on the sending end (where you already know!) but according to the docs is never null on the receiving end.
hasCategory() could be used to find known categories of external intents such as android.intent.category.LAUNCHER. This might be what I end up using but I'd rather have a general solution than try to account for each possibility here.
getPackage() looks like it might do the right thing, but unless I'm not using it correctly it always seems to return null in my tests.
Alternatively, I could always use putExtra() to add a flag to all of my internal intents, but that feels too much like a hack so I'd rather avoid it if there's another option.
Is there a simple way to find out whether an Intent is explicit or implicit?
That's not what you need, given the rest of your question. What you really should be asking is:
How can I determine internal Intent invocations from external ones, with limited hassle?
In that case, for many apps, you can use this algorithm: if getAction() is null, it's an internal explicit Intent. Otherwise, assume it's external.
If, for various reasons, you are using action strings for starting local activities (despite also setting the component on the Intent and making it explicit), then you will need to fall back to the "tack on an extra" thing. Normally, if you are starting a local activity with an explicit Intent, you are not setting an action string, which makes the action string a clear delineator between implicit (action not null) and explicit (action null).
The best way to implement this is to pass extras with the Intent that identify the Activity that called it, like you mentioned. It's not a hack in particular when you consider that Intents are frequently checked for static identifiers when the Activity finishes and the calling activity checks the result code. Yours is the inverse case of checking the calling Activity.

What is the android.intent.category.DEVELOPMENT_PREFERENCE category for?

What is the purpose of the android.intent.category.DEVELOPMENT_PREFERENCE category for?
If an activity adds this category in its intent filter, where can it be started? Thanks.
Categories are generally used by the system (or other apps) to find different kinds of Activity to do certain things. Like android.intent.category.LAUNCHER is used to find all of the Activities that can be shown on a launcher list.
From the docs:
"The categories, if supplied, must all be listed by the activity as categories it handles. That is, if you include the categories CATEGORY_LAUNCHER and CATEGORY_ALTERNATIVE, then you will only resolve to components with an intent that lists both of those categories. Activities will very often need to support the CATEGORY_DEFAULT so that they can be found by Context.startActivity()."
So android.intent.category.DEVELOPMENT_PREFERENCE is used to indicate an Activity is used for preferences during development. Since it was in API 1, it might have been included for possible future uses, of which none have been found/defined/implemented.

Android Tabs & Intents

I have a auto-complete textbox in which the user makes a selection. From here I want to load a tabbed layout which is based on the user selection. The problem is I cant figure out a clean way to pass that selection to each of the tabs. At the moment I can pass an intent to the 'tabhost' activity and then pass to each child activity explicitly, however this just seems like messy iterative code to me! So basically how can I pass my intent data bundles to the tabs activities cleanly & efficiently! Psuedo code is also very welcome ;)
Cheers guys
Sounds like you need to broadcast some information. You than will be able to set broadcast receivers in any activity/service you would like to get notified.
Read more online about Broadcastreceiver and about send broadcast
You can store the selection as SharedPreference and then just obtain the value from any activity. Second option that comes in mind is saving the selected value on the Application context which is also easily accessible from any activity.

Categories

Resources