Is there a way to set parameters that they would receive as arguments in the constructor were they initialized as standard objects instead of by intents? I can't use parameters stored in the intent because sending an intent doesn't necessarily initialize the activity / service (it may exist already). Can I use the manifest file to set custom parameters?
Thanks.
Starting an Activity will always start activity, and if activity is already in stack, then also it would launch the activity, unless some intent filter havent been specified.
For Service, if you start service by startService(), onStartCommand() method will be invoked, which has an Intent as parameter, you can get values passed from this parameter.
sending an intent doesn't necessarily initialize the activity / service(it may exist already).
What if you keep using Intents but erase the history of the TargetActivity each time you call it.
Intent intent = new Intent(this,TargetActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
I found a possible solution here: Is it possible to have custom attributes in AndroidManifest.xml tags?
Doesn't help if I want to pass objects, but for simple types it's good enough.
Related
Everyone knows if you create intent to start another activity, you pass in as a parameter into startActivity. But I just thought about the possible scenario: intent says system "call this activity", the system sees manifest and then runs activity, or this running acts internally in the app, something like "call some method of some class"?
Probably a stupid question, but I couldn't find enough info. So how does it works?
Following is the way how intent communication works:
Activity A creates an Intent with an action description and passes it to startActivity().
The Android System searches all apps for an intent filter that matches the intent. When a match is found,
the system starts the matching activity (Activity B) by invoking its onCreate() method and passing it the Intent.!
I am trying to connect to different activities from a custom soft keyboard. I need the activity underneath the keyboard to allow data to be sent without the activity creating a new instance of itself. For example: if the keyboard is over the messaging application, I want to send that application data without losing the current conversation that the user is typing into. I currently have the following code to send data to the activity.
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, screenshotUri);
//sendIntent.putExtra("thread_id", (long) 1);
sendIntent.setType("image/*");
startActivity(sendIntent);
I am getting the following obvious error when I try to run it...
E/AndroidRuntime(6129): android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
How can I keep the current activity underneath the keyboard from resetting itself when an intent is sent from the keyboard service? Or more simply, how can I send an intent from a service without setting the FLAG_ACTIVITY_NEW_TASK flag?
You can't really do this like that. A Service can't pass data to an Activity without "starting it". You don't want to do that. You want to pass data to an "already started" Activity. There's 2 ways to do this:
Use a bound Service. Have the Activity bind to the Service. The Activity can then call methods on theService (using AIDL) and receive returned data.
Use a BroadcastReceiver. Have the Activity create and register a BroadcastReceiver to listen for the returned data. In your Service, send a broadcast Intent when you want to transmit data to the Activity.
You can do this by several ways but you must set flag in order to complete task.
If you want to create a new instance and close the current instance you need to set Intent.FLAG_ACTIVITY_CLEAR_TOP.
If you want to reuse the same instance of the activity in this case you need to set both Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_SINGLE_TOPafter that it will not create another instance of the activity but call onNewIntent() of activity to run the new Intent.
This question already has answers here:
What is an Intent in Android?
(14 answers)
Closed 8 years ago.
I dont know much about intent (or android) so.. Can someone please explain me what is it exactly? i have search on the internet, A LOT.
Also what does each line of this code do?
Intent intent = new Intent (this, DisplayMessageActivity.class);
intent.putExtra("a", "b");
Thanks in advance
I suggest reading Android Intents
You couldn't have search for very long, since this is very basic topic.
I suggest you read more of Android's API guides.
Line 1 = Create message that describes what to do, in this case start "DisplayMessagActivity"
Line 2 = Add content to the message
Intent intent = new Intent (this, DisplayMessageActivity.class);
For this line, its function is to create a navigation from the current activity/page to the displaymessageactivity page.
it is like from here to there.
For this intent.putExtra("a", "b"); the purpose of this is to put like a temp storage/variable to pass to the next page for retrieval. In this case, you put the value "b" in the variable "a". With this method, you can use the value on the other activity or page.
All the above are just storing of info, it is not executed yet. if you want to execute the intent do the following
startActivity(intent);
The best example to state the behavior of Intent is it behaves like a POSTMAN that delivers message to the stated address.
Whether it may be calling service ,BroadCastRecivers ,Activity they are used in number of occassion.
Intents are asynchronous messages which allow application components
to request functionality from other Android components. Intents allow
you to interact with components from the same applications as well as
with components contributed by other applications. For example, an
activity can start an external activity for taking a picture.
Intents are objects of the android.content.Intent type. Your code can
send them to the Android system defining the components you are
targeting. For example, via the startActivity() method you can define
that the intent should be used to start an activity.
An intent can contain data via a Bundle. This data can be used by the
receiving component.
Intents can be used to start Service, call Activty, call Sub Activity, transfer the data between Activity or retrieve the data from Activity
The constructor of a pending intent needs an Intent object in it from the current context to the next activity. In my app, I have only a single activity containing multiple Views. No Second Activity.
The Views are Destroyed or made visible on demand. Obviously I cannot add a View to the constructor of an intent. So how shall I direct the pending intent? Multiple views have been accommodated into the main.xml using the <include/> command.
This is very easy to accomplish. mvnpavan already gave you good hint, you just need to do some additional work. First of all you should get familiar with this page http://developer.android.com/guide/components/tasks-and-back-stack.html.
It explains how to manage your application tasks and back stack. In your case you have 2 options:
1.Configure launchMode property of Activity in manifest. Setting singleTop should do the job since your are using just one activity.
<activity
android:name="com.yourpackage.YourClass"
android:launchMode="singleTop">
</activity>
2.Add flag FLAG_ACTIVITY_SINGLE_TOP to your Intent(the one you are passing to PendingIntent):
Intent intent = new Intent(this, YourActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Please note that in both cases you can implement callback method of you activity onNewIntent(). Use it in case you wish to do some extra work when app is bring back to the foreground.
Update:
Regards keeping connection to the server I would use Service, not Activity. Then you can do whatever you like with your activity. This is the best practise. If you worry that Activity has no direct access to the Service API, you can bind to that Service. Have a look at this page http://developer.android.com/guide/components/bound-services.html You can just copy/paste the code and your are done. It's very easy to maintain. In the future you may need to create another activity which require connection to the server. It will save you a lot of time and headaches :).
Use pending intent to resume your application and not recreate it like this:
Intent i = new Intent(this, youractivity.class);
pIntent = PendingIntent.getActivity(this, 0, i, PendingIntent.FLAG_UPDATE_CURRENT);
Hope it helps! ;)
Hi friends
can anybody tell what is the use of intent filter and what is difference between intent and intent filter
Thanks
An Intent is an object passed to Context.startActivity(),Context.startService() or Activity.startActivityForResult() etc. to launch an activity or get an existing activity to do something new.
While an Intent filter describes a capability of the component(like activities, services, and broadcast receivers )
For more details see this
Intent's are used to start new activity from the current activity. With two ways
1) Activity.startActivity(intent);
2) Activity.startActivityForResult(intent,rqwuestCode);
//The above thing's you need to put in .java file
Intent-filter you need to mention on manifeast file.
// Intent filter are used for broadcast receiver. Whenever the intent filter condition is match the android OS will launch that activity.
An intent is an object that can hold the os or other app activity and its data in uri form.It is started using startActivity(intent-obj)..\n whereas IntentFilter can fetch activity information on os or other app activities.