recover an app from stack or app in background - android

I have a small application with two activities.
The first activity has a button, pressing this button opens a second activity (the first is paused)
This second activity launch notification.
With the notification released, leave the application by pressing the "home" button and let the application on the stack or in background.
When I click on the notification, I want the application to retrieve the activity that was on the screen.
I am recovering the application using the package name:
Intent intent = null;
PackageManager manager = getPackageManager();
try {
intent = manager.getLaunchIntentForPackage("hello.com.vierco.pruebas");
if (intent == null)
throw new PackageManager.NameNotFoundException();
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.putExtra("hola", notificationData);;
} catch (PackageManager.NameNotFoundException e) {
}
I get retrieve the application, but always opens the activity number "one" because is reopening.
Someone can tell me how I can retrieve the application? retrieve it from the stack, not open it again. there is any intent?
Wear two days trying without success, I have also searched many forums but without success
A practical example would be grateful because I'm going slightly mad

Related

How to open an app via Intent and clear the back stack to return to app

I would like that when i open an app via android intent, when the user presses the back button, it will return to my app. Currently, if using the bellow code, it opens the app but when pressing the back button it returns to app main page and only after a second press return to my app.
private void openApp(String packageName) {
Intent launchIntent = mContext.getPackageManager().getLaunchIntentForPackage(packageName);
if (launchIntent != null) {
mContext.startActivity(launchIntent);
}
}
Looking online i checked guides such as:
Understand Tasks and Back Stack
I also played and checked android source code for Intent flags and actions but could not figure our how to do that
Your app can't control the workflow/ navigation behavior of another app. Once you start the other app, you have no further control of the user's navigation options until the user returns to your app.
By the way, the behavior you describe is the appropriate behavior for e.g. an app which offers an implicit deep link:
This means that when a user presses the Back button from a deep link destination, they navigate back up the navigation stack just as though they entered your app from its entry point.

How to leave the current app and launch an activity of another app?

I have a simple Android app that needs to launch another app under certain condition, and I will need to check the condition upon the app launch. That is, either continue to launch my app or just launch another app:
if (A == true) {
launch another activity of another app
leave the current app without creating the main activity
} else {
launch the main activity of the current app
}
Could anyone please let me know how to deal with A == true case? I am able to launch another app's activity but I have trouble leaving the current app without even opening the main activity.
Any help will be greatly appreciated!
You can launch other application using following intent
Intent LaunchIntent = getPackageManager().getLaunchIntentForPackage("com.example.abc");//pass the packagename of app you want to open
startActivity( LaunchIntent );
If you don't know the package name of application that you wanted to launch then try your hand on
PackageManager pm;
pm = getPackageManager();
// get a list of installed apps.
packages = pm.getInstalledApplications(0);
Pass the packagename of app you want to open
You can use this if A == true
else You can launch the MainActivity as
startActivity(new Intent(CurrentActivity.this,MainActivity.class));
If you want to start another activity of another app without the normal IntentFilter then the easiest solutions is:
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.setComponent(new ComponentName("com.anotherapp.package","com.anotherapp.package.MainActivity"));
startActivity(intent);
Of course, you would need to know the package name and the Activity name you want to start
As for finishing your application call
finishAndRemoveTask();

Marshmallow, starting SMS app via Intent and pressing back end up on home screen instead of in calling application

I'm calling SMS app with following code:
public static void startSmsIntent(Activity context, String value) {
Intent smsIntent = new Intent( Intent.ACTION_SENDTO, Uri.parse("smsto:"+value) );
try{
context.startActivity(smsIntent);
}
catch( ActivityNotFoundException e ){
}
}
It opens conversation thread in default SMS app. After pressing back button insteaad of returning to calling application, Default SMS app conversation list is displayed. Pressing back again, and I'm ending on home screen and only way to get back to my app is via Recent apps chooser. Below Marshmallow it works as expected, first back brings me to calling application screen. I tried to use extra flag:
smsIntent.putExtra("finishActivityOnSaveCompleted", true);
which was mentioned in different context in the documentaiton - here, but no luck.

Application button to open current/new instance of another app

I have two applications, A and B. In app A I've added a button which opens app B.
When the button is clicked I want to open App B if it's not already running, otherwise I want to bring the app to the front.
This is the code I use:
Intent intent = getPackageManager()
.getLaunchIntentForPackage(
"com.myapp.something");
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
The code seems to work, but the problem is that if I open app B from app A (using this code) and then click directly the icon of App B, I get two instances of the app, which is undesired.
How can I open the app, as the code does, but also get the same instance even if the app's icon is clicked?
Add Intent.FLAG_ACTIVITY_CLEAR_TOP

Android: Measuring time spent on another app from current activity

I have an activity and using an intent I call a certain portion of another app. That is lets say my app A has an activity 1 from which I call activity 1 of app B which I have no control over using the following mechanism
Activity A
Intent intent = null;
try {
intent = Intent.parseUri("URI://sample/100/1000",
Intent.URI_INTENT_SCHEME);
} catch (URISyntaxException e) {
--ERROR
}
startActivity(intent);
When I press the back button it returns to Activity 1 of my app A. Since I lose control I am not able to figure how I can capture the amount of time the user spent on the Activity from app B. Is there some way I can capture this? I know if the user goes to the home screen from that app my data will be skewed but I am ok with that. But using system time in seconds can I capture the time?
May be you can use "startActivityForResult". This way, you can write "onActivityResult" and check how much time has elapsed since you started the intent.
More about it can be found here: How to manage `startActivityForResult` on Android?

Categories

Resources