Create web page launcher, losing focus - android

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.

Related

How can you start a different activity in android according to the intent's data

I am making an application that will in fact contain two activities.
One will allow the user to log in and if done successfully will launch a web browser (MenuActivity).
The other takes a picture and uploads it to a server (PhotoActivity).
That web browser will have a link that when pressed will load my application in picture taking mode.
The flow is usually something like this:
MenuActivity->web browser->PictureActivity and then the last two steps repeat.
I have done this so far by setting my launcher activity as the MenuActivity and then putting using this code in the OnCreate override:
Intent mine = getIntent();
if (mine.getData() != null && !mine.getData().getPathSegments().isEmpty()) {
//retrieve the data here that I need
Intent i = new Intent(this, typeof(PhotoActivity));
i.PutExtra(//add here the data I received);
StartActivityForResult(i, START_PHOTO_INTENT);
}
after that I override OnActivityResult and send finish() if the requestCode is START_PHOTO_INTENT
This works correctly
my problem is that when I start the PhotoActivity from the web browser, if the user (while this is open) opens the application, then the MenuActivity shows (which it should) but when the user pressed back on it, it goes back to the PhotoActivity instead of exiting.
Is there a way to change this behaviour so that , even if the PhotoActivity is open, if the application is started manually, the PhotoActivity will not remain on the backstack ?
Thanks in advance for any help you can provide
Edit:
After reading the linked article at the solution and the answers to this question I came to the conclusion that in order to do what I want I had to set LaunchMode as singleTask in androidmanifest, and override MenuActivity's OnStop and send finish() after it was executed (if I didn't and the activity was in the backstack then it wouldn't be fired with the intent data when launched from a web page)
thanks for the help all of you
Look at the Handling affinities chapter in Tasks and Back Stack documentation. It is described how to handle this.
please do
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
This will start a new task for your application

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.

Display previous activity after launching browser activity?

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

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.

calling an activity that is in another package(android)

I want to call an activity that is in another package....
Say for example I have an Activity(activity1) in my package(package1) and I need to call another Activity(activity2) in another package(package2) from my activity1..
I have tried to import the package2 in my package1 but it did not work...
Can anyone answer me and provide some sample code?
see
Android: Starting An Activity For A Different Third Party App
final Intent intentDeviceTest = new Intent("android.intent.action.MAIN");
intentDeviceTest.setComponent(new ComponentName("fm.last.android","fm.last.android.LastFm"));
startActivity(intentDeviceTest);
where you can change the intent to VIEW depending on your case.
here is my challenging scenario..
step 1;
i have opened google.com from web browser on my emulator and opened some images in that and i pressed home key and opened my app in that i have written code for getting the top activity name and package of currently running task, i mean i got browser's top activity information...
step 2;
from that information i have lunched the browser from my activity..fine browser is launched where it was left previous(showing some images) but when i press the back key on my emulator it is not coming to my app instead it is going back to google.com home page and later if i press back key it is coming to my app....and again if i launch the browser app from my code it is launching in google home page..i want to launch it in image section as i need..

Categories

Resources