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.
Related
public static PendingIntent getActivities (Context context,
int requestCode,
Intent[] intents,
int flags)
In the doc , it says the intent[] is "Array of Intents of the activities to be launched."
It seems to be used for the case of launching multiple activities.
Could not think of a use case that multiple activities to be opened through a notification tap. Anyone has a sample for showing to launch multiple activities through one Pendingintent?
The documentation for startActivities() (which is what this eventually ends up doing) explains what happens pretty clearly:
Launch multiple new activities. This is generally the same as calling
startActivity(android.content.Intent) for the first Intent in the
array, that activity during its creation calling
startActivity(android.content.Intent) for the second entry, etc. Note
that unlike that approach, generally none of the activities except the
last in the array will be created at this point, but rather will be
created when the user first visits them (due to pressing back from the
activity on top).
So you would use this if you want to launch a specific Activity in an app and you want to make sure that the activity stack is set up appropriately, so that when the user presses BACK, it would cycle back through the stack of activities that you set up.
Personally, I understand what they were trying to do here, but I think it is a major fail and there are better ways of doing this (basically having your app be smart enough to understand how to get you back to the previous Activity without assuming that it is present in the stack). But that's just my un-asked-for opinion.
I have made a launcher that launches an Activity on home press. However, I want to launch different activities depending on different circumstances. Is it possible to intercept the home-press and assign it a certain activity dynamically within my launcher before ever stating any activity?
What is possible here? I suppose I can setContentView(something else), but I'd rather have completely different activities launched.
If you don't need to launch a Activity specifically, you could just instantiate Fragments based on your logic. Otherwise, you can have a invisible Activity that does some logic quickly, then loads another Activity.
As a launcher, you can. Have a look into the onNewIntent callback
As of GINGERBREAD, if the Activity was already created and a new
Intent is being delivered to onNewIntent(Intent), any newly granted
URI permissions will be added to the existing ones it holds.
I'm making a simple e-book reader app, and an activity can be called by many cases.
I'd like to distinguish callee activity to know its origin action in case of
From my another activity: this can be easily solved by
StartActivityForResult from calling activity.
Called by back button click from other package app after share action ("whoops, I missed to click share button, and then back.").
Switched by user's multitasking choice.
Called by user click at the start screen: this might be known by MAIN entry point at the android manifest.
How to know above cases?
I have no idea why you would need to do this but...
1.From my another activity: this can be easily solved by StartActivityForResult from calling activity.
Yes, as long as the calling Activity is your own as you can't guarantee any 3rd-party code will use startActivityForResult(...). You can, however, use getCallingPackage() and getCallingActivity() in other cases.
2.Called by back button click from other package app after share action ("whoops, I missed to click share button, and then back.").
When the user presses the BACK button your Activity isn't being "called" - it's simply being resumed or re-started. The original calling app/Activity/method will still hold true - there is no way to ascertain that this has happened as the normal Activity life-cycle methods (onStart() and onResume()) are always called even when an Activity is first created.
3.Switched by user's multitasking choice.
If you mean using the "Recent" apps view, the same applies for my answer to 2. above.
4.Called by user click at the start screen: this might be known by MAIN entry point at the android manifest.
In this case onCreate() will be called although if your Activity is simply stopped for whatever reason, it may simply be restarted depending on the launch mode you use.
In short, I can't see you being able to gather much in the way of any accurate information as to how your Activity becomes active.
I am not too sure about the actual way for the above question as I am too a new guy in android.
But to the best of my knowledge... called by back button and switched by user's multitasking leads the activity to enter pause state.
So you can access it from "onPause" method in your activity.
I ran into a similar issue with the recent list in that there seem to a handful of conditions where an app will be completely destroyed but some other process will launch the app cold from the last activity that was being used. Since my app has a state that is built up over several designated activities I need to prevent this (i.e. null refs from onCreate()).
Without checking for state is all my onCreate() functions is there a way to just prevent this?
Also, other than the launcher, recents, the back button from other apps - are there more conditions where another thing can launch my app if I have not giving manifest permission to launch it explicitly with an intent?
Thanks!
If I understand this correctly, you have initializations in activity B that rely on activity A having passed them in. If an intent launches activity B first, without it being active, or being launched by A first, your activity will crash.
Easiest solution I can come up with is make your activity A (I assume your main activity) the broadcast listener for all the intents you wish to handle, and based off the intent action, dispatch to the appropriate child activities (B, C, whatever). That way activity A does all your initialization, and you can still launch into the appropriate activity to handle the original intent you wanted to.
Alternatively. If you detect your children activities are in an invalid state, you could put initialization into a parent activity that all your activities extend from. That way you should be able to initialize properly if the activity is a fresh launch. I'm not really a fan of this, I prefer making sure my activities are dependency injected with appropriate data.
I have a lot of Activities on top of each other in my application and I want to close all these activities in one click. Can we do that.
And another thing is I want my application to start fresh each time(dont want to run in background). How can I do that?
There is no such thing as "closing" application or activities on Android. Please read: Is quitting an application frowned upon?
If you have a lot of instances of the same Activity class, then you might review your activity stack and task design.
I believe you can do it with this:
#Override
public void onClick(View v)
{
Context context = v.getContext();
Intent intent = new Intent(context, Dashboard.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);// This flag ensures all activities on top of the Dashboard are cleared.
context.startActivity(intent);
}
In this case, my Dashboard.class is the main opening activity of my application so its a way to take a user back to the start.
Not 100% sure there is a single android method that will close all of your related Activities but I think it might be possible if you use Broadcasts and broadcast receivers.
For example, if one of your Activities is currently 'on top' and the user closes it, the 'on top activity' could broadcast a close intent. all your other activities would have receivers listening for this activity and could then call finish() to end.
As for starting a fresh on each time this might be complicated. You'd have to look at overriding the 'home' and back buttons maybe but I highly advise against this. This is also complicated by the fact you want to achieve the above as well I think.