calling an activity that is in another package(android) - 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..

Related

What is base intent?

from : http://developer.android.com/guide/topics/manifest/activity-element.html
android:relinquishTaskIdentity
Whether or not the activity relinquishes its task identifiers to an
activity above it in the task stack. A task whose root activity has
this attribute set to "true" replaces the base Intent with that of the
next activity in the task.
What is base intent here?
The base intent is the root intent that initially launched your app.
Most common one is probably the one that any app has when responding to the touch on the app icon. The LAUNCHER intent.
But it can be a custom one, for example, when you respond to custom scheme/url. But here is the trick and how relinquishTaskIdentity can be useful :
Say you start your app with the launcher icon. Your base intent is now the default one.
Now, say your app is completely killed (or you've backed with the hardware icon until your app closes) and you use a custom scheme/url to open your app, at this point the base intent is not the default one. It's the one generated from the scheme/url you clicked on and may contain custom data also. Now if you just close your app with the home button and reopen it, you will just resume where you were at. But if you back, back, back... with the hardware button until your app closes there is the trick : reopening it from the recent apps/multitasking view will reuse the base intent to open it and in this particular case it will still be your custom scheme/url intent and this can be very annoying.
Why annoying? Say the scheme/url your user clicked on was used to auto login and he succeeded: you don't really want to process this url/intent again just because your user backed up until its app closed and reopened it via recent apps/multitasking view, right?
Use relinquishTaskIdentity! This is very dependent to your setup and how your app is configured as for the Activities versus Fragments, but here is an example:
In the particular case I mentioned with the auto login via a link you could have an Activity that is dedicated and is only responding to scheme/url. This same activity should be different than the one flagged as LAUNCHER. and using the property relinquishTaskIdentity="true" on it will make the base intent become any subsequently shown activity via that one.
So what's the benefit in our case? The user can't enter the app with a custom scheme/url generated intent anymore unless he/she really clicked on one supported by your app.

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.

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.

What is the way to navigate in Android app

I'm developing a Twitter client for Android and I would like to know which is the better way to build my app.
I want to have a listview that, when clicked, calls another listview to see details and so on but I would also like to be able to go back in my previous list.
What is the best way to do this in Android? At every click, I create a new intent associated with an activity and I do startActivity(intent) so that I can be able to go back with the Back button of Android.
Is it a good way or is there a better approach? I would like to navigate onward and backward of n-levels
The way to navigate back in Android is by launching the Activity intent waiting for a result Doc link and then when you need to go back from the new Activity simply finish it Doc link
Calling finish() in an Activity closes it and returns to the previous one. You can call in on a button click or similar.

Displaying app main menu/home screen when returning to app after having started an external Activity/Intent

I'm starting Android Market via my app to search for similar products using this code:
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://market.android.com/search?q=pub:\"some txt\""));
c.startActivity(intent);
This works fine for showing similar products. However, if I hit the home button while in the market, when I re-open the app it still shows market results. I want to go to the main menu in this case.
Is there a solution?
Sorry, FLAG_ACTIVITY_NO_HISTORY is probably not the correct solution. Note the semantics of it -- the activity just doesn't appear in the history. Thus if the user taps on one of the things in it to go to the next activity, then pressing back, they will not return to the previous one (but the one before). This is rarely what you want.
Worse, if they go to a second activity from the market activity, press home, and return to your app, the second activity will still be there (it is keeping itself in the history).
The correct flag for this situation is FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET.
If you add the FLAG_ACTIVITY_NO_HISTORY flag to the intent, it won't be kept on the history stack. When the user navigates back to your application, the last activity that was visible before you launched the marketplace will be shown.
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://market.android.com/search?q=pub:\"some txt\""));
c.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
c.startActivity(intent);
Edit: hackbod is correct: FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET is a better fit for what you need.
This is not a problem.
When you press home on the Market app it isn't closed, just paused. So when you open it again you resume it. Check Android activity's lifecycle.

Categories

Resources