Display previous activity after launching browser activity? - android

I'm got a scenario where my app has a series of activities and then opens a Browser activity. The browser activity then authenticates the user and calls back to a URL with custom scheme i.e. myapp://finished.
An intent filter is used to trigger the display on one of the existing activities. I basically want the app to go back to the activity that was displayed before the Browser activity was launched.
The problem I'm having is that the Browser activity creates a new task so when the browser calls back and my activity is loaded a new instance of it is created in the browsers task and not my app's orignal task. This results in my activity being recreated.
Task (created by my app)
1) Activty 1
2) Activty 2
Task (created by the browser)
3) Browser Activity
4) Activity 2 (new instance)
I'm aware that there are flags that can be used to resume existing activities instead of recreating them but they don't work as any new intent that is created after the browser is restricted to the browsers task stack.

I guess by finishing the activity just after getting invoked by webview will pop up your last activity on the stack.

Are you using a Webview or launching a 3rd party browser? In the last case, consider using a Webview

Related

Why android deeplink didn't jump to the correct activity?

I have two apps, click the button in app A will jump to app B due to deeplink. And in app B, there are two activities including SplashActivity and MainActivity, where I set the intent filter in SplashActivity that deeplink will bring it to in app B when the button in app A is clicked. And it worked fine for most time.
However, here is the situation. I open app A, click the button, and jump to app B's SplashActivity, which start MainActivity in a while and then finish itself. Till now it works fine, however, when I switch front app to app A, which means app B is in background now, and then I click the button again in the app A. This time it doesn't jump to SplashActivity(onCreate() and onResume() of app B are not called), but to MainActivity by calling it's onResume() callback, and the intent.data, which is sent from app A, is find to be null.
So why the deeplink didn't bring the app to the expected activity in the situation above? Begging for any answer!
When opening the deeplink while app is in background, doesn't launch the new app instead it open the current instance of the app. This is to avoid multiple instances of the same app.
In the manifest file, you need to add the follow to your main activity.
android:launchMode="singleTask"
With this, if an instance of activity already exists at the top of the current task, no new instance will be created because it will fire off an onNewIntent() method instead of creating a new object.
You can fetch and handle your intent data in onNewIntent()

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.

Home button and back to app in a newly installed app loads the initial activity again

I'm encountering the following scenario in my application:
Installing it and immediately opening it (using the open button
after the installation, instead of the icon in the applications
list).
Navigating through a few activities.
Clicking the home button.
Clicking the application icon to load it again.
Instead of returning to the activity I was in, the initial activity
is loaded. The previous activity is still there and can be accessed by clicking the back button, but there isn't really a way to know that and it looks like the app was completely restarted.
This may be an issue in this app, because in some cases it might require a registration after performing a few actions, and losing the focus on the activity could be very annoying
Is there anything that can be done to avoid that?
There's a couple of flags under Intent that might be helpful. You use them when an Activity is launched:
"FLAG_ACTIVITY_TASK_ON_HOME: If set in an Intent passed to Context.startActivity(), this flag will cause a newly launching task to be placed on top of the current home activity task (if there is one).
FLAG_ACTIVITY_REORDER_TO_FRONT: If set in an Intent passed to Context.startActivity(), this flag will cause the launched activity to be brought to the front of its task's history stack if it is already running."
http://developer.android.com/reference/android/content/Intent.html

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.

Create web page launcher, losing focus

I have created a php webpage. I now want to create a launcher application in android phone, it simply opens the browser with the url "http://<mywebsite>/m".
I use uri intent to launch the browser in onCreate function.
public void onCreate(...)
{
....
startActivity(new Intent(Intent.ACTION_VIEW, new Uri(http://<mywebsite>/m)));
...
}
I execute this program in my G1 phone (Cyan Mod 5). However, when I click the "Home" key, and then re-enter my application through Task list, I lose focus for my last started browser, and the screen blank
Any suggestion?!
Should I need any code in onResume function to re-focus my web!?
If you call finish() after startActivity, your 'redirect' activity will be closed and removed from the task's activity stack, leaving just the browser activity. For more on the basics, read the developer guide section on activities and tasks.

Categories

Resources