How to invoke any app without losing its previous state? - android

i am able to launch any apps which are on emulator screen from my app just with the help of their package names but after launching those apps if i press home button and if i launch the same app from my app it's being launched from initial state not where i used and left that before...for that i tried setting the FLAG_ACTIVITY_REORDER_TO_FRONT on the intent which is used to launch the app but there is no effect...
Here is my code
PackageManager packageManager = getPackageManager();
Intent intent=new Intent();
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent=packageManager.getLaunchIntentForPackage("com.android.email");
startActivity(intent);

You don't get used of FLAG_ACTIVITY_REORDER_TO_FRONT, because you're rewriting the intent object. Fix your code to:
PackageManager packageManager = getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage("com.android.email");
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Hope, this helps.

Related

Android - can't go back to mainActivity after starting a different app

Okay, I think I'm missing something here but can't seem to find a way around it :\
So this is my scenario, I have two apps, A and B. A Opens a B with the following intent:
PackageManager pm = getPackageManager();
Intent n = pm.getLaunchIntentForPackage(currAppInfo.getName());
n.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(n);
(currAppInfo is a custom object and getName returns the package name.)
Anyway, B is installing APKs. A receives the package installed broadcast and should be now moved back to front, how ever if I'm starting app A with an intent
Intent serviceIntent = new Intent();
serviceIntent.setClass(context, MainActivity.class);
serviceIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(serviceIntent);
Instead of seeing A's mainActivity screen all I see is B's main activity screen.
Why is that? Is it the way I open the apps with the intents or am I missing something more basic here?

How To Open Homescreen Activity for Selecting Launcher in OPPO or Other Phones

Hello Friend I'm working on Launcher Application. for select my launcher as Home app a popup is showing in samsung. by using this code.
private void launchAppChooser() {
PackageManager packageManager = getPackageManager();
// get dummyActivity
ComponentName componentName = new ComponentName(context, DummyActivity.class);
// enable dummyActivity (it starts disabled in the manifest.xml)
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
// create a new (implicit) intent with MAIN action
Intent intent = new Intent(Intent.ACTION_MAIN);
// add HOME category to it
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// launch intent
startActivity(intent);
// disable dummyActivity once again
packageManager.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}
but this code is not working on OPPO and HUAWEI. I'm using following line for showing same popup to select home launcher
startActivity(Intent.createChooser(intent,"Select Launcher App"));
This code is showing popup with launcher Apps but when i clicked on any app its not set as default.
Can i open homescreen Activity from setting in OPPO for choosing default launcher like this(please open image). because CM Launcher app is opening this activity
Clicke here to open image
Now i Want To Open This Activity Programetically. and Need Yours help for doing this.
For checking in oppo devices, you have to check if the current default home app package name contains "oppo.launcher".
After that, use this code using it. You can open the default home apps settings in "oppo devices"
Intent intent = new Intent();
intent.setAction(Settings.ACTION_HOME_SETTINGS);
startActivity(intent);
This is an alternative solution for changing the current home app to your launcher app. This intent redirect user to default launcher app list in oppo devices.

How to launch another app or bring it back to foreground like Android launcher?

I am developing two apps that will be installed on the same device. My customer wants a shortcut button on each to jump to the other app in its current state. This action would duplicate the behavior of pressing HOME then pressing the other app's launcher icon. If the app has not been started, it would start it. If the app has already been started, then the current activity is resumed. Each app has many activities, so the current activity at the top of each app's task stack would be unknown at run-time. I have searched all over and have not found this problem answer sufficiently. I have tried variations on this code without success:
Intent intent = getPackageManager().getLaunchIntentForPackage("com.example");
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
I am at a loss, any help is appreciated.
I figured out my problem. Here is what works:
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.example", "com.example.MyActivity"));
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

How to launch a mobile app from another app?

Is there any way to launch for one mobile app to launch another mobile app, e.g. via a button click?
Example: the org.apache.cordova.camera plugin allows direct access to the camera on a button click. In the same way, how can one app launch another app?
You can use this java code:
Intent LaunchIntent = this.cordova.getActivity().getPackageManager().getLaunchIntentForPackage("appPackage");
this.cordova.getActivity().startActivity(LaunchIntent);
or try any of these 2 plugins for launching apps:
https://github.com/lampaa/com.lampa.startapp
https://github.com/dmedvinsky/cordova-startapp
Try this
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("package name here");
startActivity( LaunchIntent );
If you don't know the package name of application that you want to launch then try this
PackageManager pm;
pm = getPackageManager();
// get a list of all installed apps then launch by pakagename.
packages = pm.getInstalledApplications(0);
String packagename = packages.get(position).packageName.toString()
Refer this android-package-manager
You need to find out full name of application, and then start it as activity via intent like this:
Intent myIntent = new Intent(getApplicationContext(), "full name of activity you are starting");
startActivity(myIntent);
You can even receive result from that activity check this. Hope this helps!

Close the app programmatically (know package name)

I use the following code snippet to launch an app on device:
Context mContext = getContext();
String packageName = getPackageName(); //the app to launch
Intent mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if(mIntent!=null){
mContext.startActivity(mIntent);
}
It works, the app get launched, however, I don't figure out a way to close the launched app by using the packageName.
How to close the launched app if I only know the package name?
You cannot close another app. Only the system can do that.
But if you are also the author of that other app, you could create a receiver in that app's activities that accepts an intent that tells it to finish() the activities.
You cannot close other application from your app.your problem has a workaround,after starting app based on package name you may send home intent which will give same experience to user.
Intent mIntent = getPackageManager().getLaunchIntentForPackage(packageName);
if(mIntent!=null){
mContext.startActivity(mIntent);
}
//sleep for 250ms or whatever time using handler
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);

Categories

Resources