difference between intent and startActivityForResult in android? - android

Actually I want to know the work of intent and startActivityForResult.Its little confusion on this topic.

Intent is a mechanism for communicating between different components of android (i.e. Activity, Service, BroadcastReceiver, etc.)
Where as startActivity() and startActivityForResult() are used to start another activity.
startActivity()-use to start another activity
startActivityForResult() - use when you are expecting some result from the activity you want to start.

Related

Start activity for result when started activity is not the "returner"

Let's say I have four activities: StartActivity, ConfigureActivity, GameActivity, and ResultActivity. The flow of activities is as such:
StartActivity starts ConfigureActivity with intent
ConfigureActivity starts GameActivity with intent
GameActivity can go back directly to StartActivity by starting it with intent
GameActivity can start ResultActivity with intent
ResultActivity starts StartActivity with intent
While in GameActivity, the user has the opportunity to gather Strings that he or she want to save for later use. These are added to a simple ArrayList of Strings which I will later need in StartActivity.
From GameActivity, the user might either start the ResultActivity, or exit the current activity to go back to StartActivity.
When going from GameActivity to ResultActivity, and then from there back to StartActivity, the ArrayList is passed along the way through intents. On regular exit, which is done by listening to onKeyDown, an intent is in the same way (data is passed back to StartActivity).
Now, I know you can use startActivityForResult to start an activity with excpectation to get something back. But I since my StartActivity actually launches ConfigureActivity, and that ConfigureActivity will not at all be involved with this data, how can I do this? I use SharedPreferences for application settings, but there I have xml-files with items that will never change. In this case, the list might vary from 0 to maybe 100 strings. Should I use sqllite?
Also, I am not providing any code because I am only looking for some conceptual guidance and I do not specifially need code example as response to this question.
Only the StartActivity has to create a new Intent.
All the other activities use the same intent.
At forwarding while starting a new activity but also used as result intent for setResult().
Different activities just use intent.putExtra() to put extra data.
In this way all putExtras() keep remained and every activity can read the putExtras of all other activities.

Using Intent to Update UI

I have a networking app that needs to update the data counter in the app's UI as bytes are being sent and received simultaneously. Have tried implementing an intent which carries the code this way:
intent.putExtra("received", last_rx)
intent.putExtra("sent", last_tx)
The issue is I can't call setResult() as the method where this intent is placed is not a View meaning I can't get the intents from onActivityResult. Is there a workaround or a better way to update my UI.
Please note that I call the last_rx and last_tx from other classes as a double like this::
myclass.last_rx += s.length();
I think you should try EventBus. It won't break your logic. EventBus a really nice, easy to use "publish/subscribe event bus optimized for Android".
If you really want to use intents for communication you have 2 options:
start activity with "singleTask" or "singleInstance" mode. New
activity wont be created and you get your intent in onNewIntent()
method. (bad way)
register local broadcast reciever in your activity. See docs how
to use it correctly.

How to stop an activity started with implicit intent (Intent.ACTION_DIAL etc) from a service

I would like to stop an activity started with implicit intent(using Intent.ACTIN_CALL etc). Is there a way to do this?
Also when we use "startActivity()" function of Context(abstract class), how does it actually start it?
Thanks.
No, you cannot stop an activity.
To answer your second question, when you call startActivity() the Intent that you pass as a parameter is given to the Android framework. The framework performs the appropriate Intent resolution using the contents of the Intent to determine which activity to actually start. Then, depending on the activity that needs to be started, it may need to create a new OS process, instantiate the Application object for that application and finally instantiate the activity object and call onCreate() on it. However, there are other things that may or may not happen during this process, depending on the state of the task that contains the activity, the Intent flags used, the launchMode of the activity as defined in the manifest, etc.

Prevent intent from being started

Is there a way to prevent an intent from being started?
I'm working with a buggy third-party component that launches 2 similar intents in a row. This causes my app to display two activities, one on top of the other, and the user has to press back twice to return to the original activity.
I was wondering if I could circumvent this behaviour somehow. Is there some kind of intent listener where I can cancel the extra intent before actually being started?
More context:
It's an ACTION_SEND intent.
I can't modify the manifest of the activity it calls.
I can modify the source cody of the activity that will be paused.
I have access to both intents but I can only modify the first one. The second one I would like to cancel.

Android - Call StartActivity without navigating to it if it is started but paused

I have a foreground service that calls StartActivity on ActivityA. If ActivityA is not in the foreground, it is displayed to the user. If the Activity is in the foreground, the service still calls StartActivity, and the activity receives a new Intent and updates itself accordingly.
The user may interact with ActivityA which will display ActivityB as a popup (Theme.Dialog), during which ActivityA is paused. Now if the service detects another event, it calls StartActivity again. This time though, I do not want to navigate to ActivityA. I want the user to still see the ActivityB popup and have ActivityA update itself in the background.
Is there any flags I could add to the intent which the service starts which will prevent navigation to ActivityA if it is Started but Paused?
Other Info :
1. If I have ActivityA bring ActivityB back to the foreground, ActiviyB end up flickering and just doesn't look good.
2. Have the service use ActivityManager to determine if ActivityB is displayed. If it is, broadcast an intent which will be listened to by ActivityA and force it to update, instead of calling StartService. But this couples the Service with two activites, needs more coding, and will force me to synchronize the service call onto the main thread to avoid timing issues.
I am really hoping there is something simple that I can do like set some flag on the intent. Or catch the intent within ActivityA, update the activity, but cancel the intent to prevent the screen navigation.
Any suggestions?
Note: Not using Android notification bar. Understand that starting activities from service is not recommended. We have a specific need for this.
Yes you can, if you are binding your service.. call bindService(mConn); in onResume() of your activity and unBindService(mConn); in onPause()..And in your service class, #Override the onUnbind() method and you can set the flag there.
I hope it was useful.
It sounds like you might want to use AtomicReference from java.util.concurrent.atomic to pass the right intent to start activity. I haven't actually tried this, but its probably a good place to start.

Categories

Resources