How to tell when user selects "Open in Chrome" from menu - android

I'm trying to determine when the user opens a Chrome Custom Tab in Chrome (the "Open in Chrome" option from the menu).
My navigation callback returns an event code of 6, which is the same code returned when the user closes a Custom Tab. Is there a way to differentiate between whether the user has closed the Custom Tab or opened it in Chrome?

Navigation code 6 means that CustomTabs Activity is not visible any more for either user has navigated back to the activity that started the CustomTabs intent or another activity, in this case Chrome has been started, has taken place.
When the user navigates from CustomTabs activity to Chrome you get navigation code 6, when back button is hit, another event is sent with code 5 (tab visible again). In this case you are the CustomActivity is still visible, previous activity was finished, the activity that started the intent is still paused.
Starting CustomTabs for activity might solve your case when you have navigation code 6 and onActivityResult() method called on the activity that started the session.
public void openUrlForResult(String url, int requestCode){
CustomTabsIntent customTabsIntent = buildCustomTabIntent(mCustomTabSession);
customTabsIntent.intent.setData(Uri.parse(url));
mContext.startActivityForResult(customTabsIntent.intent, requestCode);
}

Related

Activity not closing properly

I am calling one activity on click of status bar notification which is having a Complete button. on click of btn. i have folllowing code -
public void completeTask(){
taskDBAdapter.deleteReminder(rowId);
taskDBAdapter.close();
Intent intent = new Intent(this, TaskManagerActivity.class);
startActivity(intent);
finish();
}
whhen i click complete btn new activity (TaskManagerActivity) gets opened properly.But if i reopen my application it still tries to open this activity and not my default landing activity. Any help on this.??
EDIT -
I have tried repositioning my finish() statement . Still its not working.
EDIT 1.1 -
Ok I will provide some details here. Assume my app has two activities
Main Activity
Notification Activity
My app create some notification to display on Status bar. So as soon as i click on status bar Notification actvty will open. Now there is a button called Complete on click of which the code given will fire and main activity (in the code TaskManagerActivity.class) will open. But after I press back button in my app and again reopen it , it opens the notification activity when it should have fired the main activity (as it is launching activity).
Thanks,
Ray
That's the default way android functions. If you press the home button and then open your app again, it will restore the apps previous state (unless it has killed the apps processes and activities due to memory constraints). So you are not actually restarting your app but only restoring it.
If you wanna quit the app, then press the back button. Now when you re-open the app, the original activity will be launched.
Do not modify this behavior. It is the default system behavior and users expect it to work this way. Your app is fine :-)
- First of all the behavior which you are experiencing is the way Android is made to function, moreover when a user gets a call while this app is open, after finishing the app he will definitely want to get back to the state where he left the application.
Still if you want it that way, here it is.....
- Make sure your application has only single instance of it running, by using android:launchMode="singleTask", or android:launchMode="singleInstance"
- Then finish() your Activity at onPause().
#Override
void onPause()
{
super.onPause();
finish();
}

Open Browser as Intent but don't keep it on the Activity stack

I'm having some problems with understanding the activity stack and the behaviour of how it affects my app.
Upon clicking a button it starts an Intent which opens the browser. When I'm in the Browser and I press the home button I land onto the homescreen. Now if I start my app again via launcher it opens the browser instead of my app. How can I circumvent opening the browser upon launching my app?
Right now, the code to open an url looks like this:
private void openUrlExternal(String url) {
Intent openUrlIntent = new Intent(Intent.ACTION_VIEW);
openUrlIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
openUrlIntent.setData(Uri.parse(url));
startActivity(openUrlIntent);
}
Am I using the wrong flags? If so, what flags do I have to use?
Thanks in advance!
Try like this:
openUrlIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
openUrlIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
That should disassociate the browser task from your own which means when you re-launch yours it should go to your Activity instead of the browser.
However it also depends on where you are calling openUrlExternal() from. If you call this when your activity launches it is still going to take you back to the browser, but if you call this from an event listener (i.e. Button click) then it shouldn't get called when you re-launch your app.
I don't think the accepted answer is exactly correct. It depends on what you intend (no pun intended, heh) to do.
Using Intent.FLAG_ACTIVITY_NEW_TASK it means that the launched activity is completely separate from the launching one. In particular, you can switch to the old activity with the Apps button without exiting the new one.
Using Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET means that the user will be returned to the previous activity when it's launched from the apps drawer.
In both cases launching the app again will get you to the previous activity. The main difference will be whether both activities (or just the last one) are shown in the app switcher.

android complete action dialog callback

I've such situation:
By clicking on button from activity we should open browser, and finish activity.
But, if the user has more then one browser Complete Action Using dialog is showing, so user can complete action by choice or return to application by pressing back.
I should finish activity only in case that browser opened.
Is there any way do determine that intent delivered succefully?
Is there any to determine if Complete Action Dialog has been cancelled by pressing back?
If none of above, any suggestions reagards solving this problem?

Launch browser from within app - how do you get back to the app?

I've got the following to open up a browser from within an Android app.
String url = "http://www.example.com";
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
How do you get the user back to the app after they have viewed the page?
Edit
I have android:noHistory="true" set in my manifest for the Activity calling the Intent.
If the calling Activity is in the Backstack (as default) the user would press "back" to peal the top layer off the stack.
However if the browser closes, the Activity does too, and the last Activity in the Backstack comes to the fore. If you own the site your going to and can put a "back" button in it (With javascript window.close() or similar), the activity will close and your applications topmost activity in the stack will resume.
If your Activity isn't in the backstack then I would suggest instead of sending the user to the browser Task use a custom Activity containing a WebView giving you full control (such as manually starting the original Activity through an Intent)
You can't. They have to press the back button to get back to your app.

Bug in ActivityManger in android 2.1 when using long press on Home Key

In one of my services I fire an event that puts a notification in the status bar which includes a pending intent to start an activity when the user clicks the notification.
The activity I'm starting is actually a "popup" that has the theme of a dialog popup (android:theme="#android:style/Theme.Dialog") defined in the manifest. The code for the pending intent is below:
Intent intent = new Intent(this, PopupWindow.class);
PendingIntent launchIntent = PendingIntent.getActivity(context, 0 , intent, 0);
notificationManager.notify(notificationRef, notification);
Everything works fine in android 2.2, but when testing in android 2.1, the newly started popup window doesn't take focus on the screen.
I know the activity is starting, because if I hold the home button down to bring up recently started apps, the "popup" will magically appear and takes focus.
Is there something I'm missing here? Why does my code work in android 2.2 and not 2.1?
After a day of debugging, I found that the new activity launched from the pending intent (Activity B in the stack) would get lost somewhere in the ActivityManager behind either the already opened activity (Activity A) or possibly the window of recently opened apps (long press on home key).
This happened ONLY if navigated away from Activity A with the use of a long-press on the home key ONLY in versions of android < 2.1. Every other instance of navigating away from Activity A (home key short-press, back-key press) would allow the pending intent on Activity B to open and take focus above everything on the screen. In android 2.2 and above, the code works absolutely fine with no issues. Very weird. To make things even weirder, if I put a Toast message to be displayed within the onRestart method of Activity A, the issue completely disappeared. There is nothing weird going on inside the onPause method of Activity A either....I still don't know.
I tried almost all flags on the pending intent for Activity B, but none of them would allow the popup to get to the top of the activity stack....I think Nanne and willytate put me on the right track...
I abandoned the method of setting the pending intent inside of a service, I think it was breaking the affinity between Activity A and Activity B. When I set the pending intent for Activity B inside of Activity A, as apposed to inside the service running in the background, Activity B (in the form of a popup via android:theme="#android:style/Theme.Dialog" in the manifest) would always appear at the top of the stack.
Once again, this "loss of focus" on an activity has only ever happened to me in this process:
Android 2.1
Launch Activity A
Use service to set status bar notification with a pending intent to start Activity B
Navigate away from Activity with long press on home key to any other application.
Service fires notification in status bar with intent to start Activity B.
Click notification to open Activity B.
Activity B, created as a dialog, is no where to be found and Activity A comes to the stop of the stack, but remains paused. No response to user touch at all on Activity A and the timer being displayed isn't moving. The Activity is stuck in onPause maybe?
The only way to get Activity B to appear is to, once again, long press the home key. When this is done, I see the following on the screen, in descending order with the first as the one at the top of the stack ready for user input :
List of recently used apps.
Activity B.
Acitivyt A.
Pressing the back button to dismiss the list of recently used apps will allow Activity B to take user input, and from there everything runs normally.
If this was not a complete waste of time, I did get a much better understanding on how Android handles the application stack.
Can anyone else (if they really wanted to) re-create this issue?

Categories

Resources