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.
Related
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.
I have a heavy-duty init process that I would like to run in 2 different places, depending on how the application started.
The heavy-init runs in background with an AsyncTask.
The cases are:
if the application was started from a Widget (via Intent), I need to make the heavy init inside the Application.onCreate
in any other cases, I need to make the heavy init inside the Activity.onStart
In other words, I'd like to know if inside the Application.onCreate there's a way to retrieve the triggering Intent.
thanks
Fabio
As it is reffeered in this link : Android- How to check the type of Intent Filters Programmatically?
You can use the getAction() of your intent to know what type of intent launched the activity.
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.
I'd like to pass some data from a foreground activity to another that is currently in background.
I know that I can use setResult()/onActivityResult() and startActivity()/getIntent() to send an Intent with data while an Activity opens/closes, but in this case this does not seem to be usable.
Is there any way other than using static methods or fields?
you can use sharedpreferences to store the data
I haven't done what you are trying to do, but I think you may want to investigate Handlers and message passing...
BroadcastReceiver (registering it in onCreate and unregistering it in onDestroy) was exactly what I was looking for.
I'm using a singleTask-mode activity that handles a certain type of files by specifying a intent-filters.
I need to handle each such file Intent exactly once. The problem is onNewIntent() is only called if the task is already alive, forcing me to handle the intent from onCreate() too. Unfortunately, onCreate() gets called for a whole bunch of reasons (e.g. screen rotation), and the Intent returned by getIntent() may be the same one across several onCreate()'s.
Of course, it is possible to work-around this using some ugly hack, but I was wondering what the elegant solution would be.
So far the best solution I came up with is to setIntent(new Intent(Intent.ACTION_MAIN)) every time after handling an intent. This is a similar pattern to how web servers redirect you to a GET page after a POST page to avoid redoing an operation as result of refresh.
Thanks!
I am not sure If I get your Problem, but once you call an intent and start the new activity,
immediately after call finish(); to end the activity you are leaving. This will end your last activity and will prevent from multiple activities from running at the same time.
Also if you are using screen rotation as a way to launch activities, you can always control which one's you do not want to start multiple times by setting some checks using "If and Else" Statements.
remove singleTask will solve your problem;
call remove singleTask's activity, can replace with:
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
How about setIntent(null) after handling it?