How to resume an application from another application? - android

Actually, I know how to launch an application from another application.
The problem is that the application always restarts instead of resuming when I launch it from another application after launching it from home screen. (I mean the application is running first by pressing the shortcut from home screen)
For example,
There are two applications : A, B
Launch two applications from home screen first.
Launch A application again from B application.
A application restarts instead of resuming.
How could I resume it?
Now I am doing it as below.
Intent intent = getPackageManager().getLaunchIntentForPackage(PACKAGE);
intent.setAction(ACTION);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
ACTION is custom action.
Please help.

You can check for the Intent.CATEGORY_LAUNCHER category and Intent.ACTION_MAIN action in the intent that starts the initial Activity.
If those two flags are present and the Activity is not at the root of the task (ie.. app was already running), then I call finish() on the initial Activity. That exact solution may not work for you, but something similar should.
Here is what I do in onCreate() of the initial/launch Activity:
if (!isTaskRoot()
&& getIntent().hasCategory(Intent.CATEGORY_LAUNCHER)
&& getIntent().getAction() != null
&& getIntent().getAction().equals(Intent.ACTION_MAIN)) {
finish();
return;
}

You need to set the ACTIVITY_REORDER_TO_FRONT flag on your Intent when you call Context.startActivity().

Related

How to modify intent when "Don't keep activities" is enabled?

The app has SplashActivity which is launched both normally and via deep link. In later case the SplashActivity creates an Intent, sets the data from deeplink, and launches MainActivity. MainActivity checks the data and behaves differently for normal launches and deep link launches.
When I enable "Don't keep activities" and launch the app via deeplink then when I go to background and return to the MainActivity it is recreated with deeplink intent.
I tried to modify the Intent in onDestroy and onSaveInstanceState with removeExtra and setIntent, but when I go to background and back the intent is still the same from deeplink and the extra is still there. I also tried to check intent flags for LAUNCHED_FROM_HISTORY but when I debugged the app flags were 0 every time.
I don't need the app to behave like it was launched with deeplink when it was launched from history. Is it possible to preserve intent modifications in that case?
You need to modify your intent in onPause, everything you do after onPause will not be saved, you may even consider modify your intent just after you consumed it to make your deeplink, i guess this will work as well
In onCreate block, Try removing the deep-linking keys in the Intent that you send to the activity
int flags = getIntent().getFlags();
if ((flags & Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY) != 0) {
// The activity was launched from history
// remove extras here to prevent the app from retaining the deep linking intent
getIntent().removeExtra(key);
} else {
// get data from intent normally
String s = getIntent().getStringExtra(key)
}
This should prevent the Activity from retaining the previous Intent.

An android OS level bug launcher activity launch always

When we install the application from source other than Google Play Store, then there are two options, Done and Open after installation finished. Then app's behavior is different in both cases. When we click on Done it works perfectly as expected but clicking on Open button the launcher activity is placed on the top of the activities stack every time app switches from background to foreground (even after application has been forcefully killed) when clicking on the app icon only and not from recent app tray.
You have to check whether the launcher activity is placed on top of the activities stack and clear the or finish the activity if it is on top of the stack-
Put below code on the onCreate() of launcher activity-
final Intent intent = getIntent();
if ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0
&& intent.hasCategory(Intent.CATEGORY_LAUNCHER)
&& intent.getAction() != null
&& intent.getAction().equals(Intent.ACTION_MAIN)) {
finish();// finish this launcher activity
return;
}

Resume another application from the background

I am trying to resume another another android application from the background programmatically. Is it possible ? How can I do it ?
just launch another app.
Intent intent = getActivity().getPackageManager().getLaunchIntentForPackage(another app packagename)
start this intent.
(you can also use intent by URI .And make sure suitable lauchmode)
If you mean something like: when user presses home or back button your application launches another application, then you can use onStop() or onPause() methods.

Two instances of my android application are running....How to avoid this?

Here is my problem -
I copied my .apk file onto phone memory card and launch my application clicking on it and it allows me to install my application.I install my application.Finally,I got system installation pop up containing two options "Open" and "Done".When i click "Open" my application got launched.Up to this point everything is working without any problem.
Now in my application I click on a button and some download is taking place as a result(Showing progress dialog).Now I press a Home button,so my application goes to background.
Now I again launch my application by going inside Menu and clicking on my application icon.
Expected result - Still I Should see Progress Dialog for downloading.
Actual result - A new instance/session of my application is getting started.
So how to avoid this so that only one and one instance/session of my application should run.
#Palejandro, here you are. Put the code below into your main activity onCreate() method:
// Possible work around for market launches. See
// http://code.google.com/p/android/issues/detail?id=2373
// for more details. Essentially, the market launches the main activity
// on top of other activities.
// We never want this to happen. Instead, we check if we are the root
// and if not, we finish.
if (!isTaskRoot()) {
final Intent intent = getIntent();
final String intentAction = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && intentAction != null && intentAction.equals(Intent.ACTION_MAIN)) {
Log.w(TAG, "Main Activity is not the root. Finishing Main Activity instead of launching.");
finish();
return;
}
}
I used this piece of code in my projects and it works fine!
I believe you need to put
<activity
android:launchMode="singleInstance"
</activity>
in the manifest file.
what do your OnPause, OnResume and OnCreate?
I will bet money you are not saving anything OnPause, and starting a new instance all the time via OnCreate.
You should read the notes on Activity Lifecycles.
If you haven't got this sorted yet, I would say your app is actually being killed when home is pressed, or perhaps you have a bug that doesn't latch onto whatever object is keeping state.
// put below code in your launcher activity before call super and setcontentview()
ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);
// get the info from the currently running task
List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(10);
boolean alreadyTask=false;
for(ActivityManager.RunningTaskInfo info : taskInfo){
ComponentName componentInfo = info.topActivity;
String value= componentInfo.getPackageName();
if(value.contains(getPackageName()) && !info.topActivity.getClassName().contains(getPackageName()+".LauncherActivity")){
alreadyTask=true;
Log.i(TAG, "second instance found!!!");
break;
}
}
if(alreadyTask){
finish();
}
I don't have a solution but the problem is that the intent used to start the app is different when you open it directly from install compared to opening it from your home screen. Since it will get started by two different intents it will open a new instance the second time round.
A quick work around is to avoid pressing "Open" when you have installed the application. Press "Done" and then find the application yourself.
See: http://code.google.com/p/android/issues/detail?id=2373

How i can close/exit button on my app?

i have a button to close my app with this code:
finish();
the problem is that this button doesn't exit of my app... it simply closes the current intent ant returns to the previous intent (window) of my app....
how i can do a real exit/close button?
i tryed with this:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
but it doesn't works, because when i turn back into my app, the app comes in the last opened window, and not in the first window of the app, how i can do that? i need that when i re-open my app it starts on the first window of my app
If you really want your app to die. You could initiate each intent with startActivityForResult(). then before each finish() set the result to send back. in each parent activity you can override onActivityResult() to test whether the result received means the application needs to end. if so you can call another set result and finish(). repeat this in all activities and you will find that your application terminates entirely.
Incidentally I'm writing this from memory. function names may not be exact.
Hope that helps.
p.s. re-read your requirements. you can always stop the finish loop at your first activity.
I would do it this way:
I would define my initial activity (i.e. MainMenu) with a Launch Mode of singleTop
I would then invoke my MainMenu from the activity that is going to close the application.
startActivity(new Intent(this, MainMenu.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("closeProgram", true);
Then override the onNewIntent in the MainMenu activity; check for the extra boolean of "closeProgram", if the value is true, then do a finish();
Haven't tried it but I think it should work.
I recommend you read this: http://blog.radioactiveyak.com/2010/05/when-to-include-exit-button-in-android.html
Chances are, you don't want an exit button. Perhaps a logout button, but that's it.
finish() closes activity (this is what you call intent, but it's not correct), not application. Application can not be finished at all (only forcefully killed like task killers do). You should design your activity stack in such a way that it will suit your needs. May be you should look at stack rearrangement examples in ApiDemos.
you can try this: System.exit(0);

Categories

Resources