Issue when newly launched app from my application crashes - android

I am developing an application which can launch number of other applications. I can successfully launch new applications from my app. The problem is when the launched application crashes, my application is taken back to the previous screen(From the screen where new app is launched).How can i make sure my app stays in the same screen even if the newly launched app crashes.I tried to set the intent flag Intent.FLAG_ACTIVITY_NEW_TASK while launching new app but this didnt work.
Intent LauncherIntent = getActivity().getPackageManager().getLaunchIntentForPackage(package.trim());
if(LauncherIntent != null){
LauncherIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(LauncherIntent);
}
This code snippet is used to launch the new app.The 'package' will have the package info of newly launched app.

Try this:
Intent LauncherIntent = getActivity().getPackageManager().getLaunchIntentForPackage(package.trim());
if(LauncherIntent != null){
LauncherIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getActivity().startActivity(LauncherIntent);
finish();
}

Related

Android Studio startActivity

I was searching by myself but failed. Can I start an application like Google translator in the background? Here below-listed program runs the Translator very well, but at the same time, my app goes to the background. I would like to have my app foreground while the Translator is started in the background. Many thanks in advance!
Intent app_to_launch = getPackageManager().
getLaunchIntentForPackage("com.google.android.apps.translate");
if (app_to_launch != null) {
startActivity(app_to_launch);
}
Updated:
Following the subject. One thing is observed. Here below-listed code is working fine:
Intent app_to_launch = getPackageManager().getLaunchIntentForPackage("com.google.android.apps.translate");
if (app_to_launch != null) {
startActivity(app_to_launch);
}
try{Thread.sleep(1000);}catch (Exception e){};
Intent intent = new Intent(MainActivity.this, WordsStatus.class);
startActivity(intent);
But if I remove the 1 sec pause (Thread.sleep) there is no sign that the Google translator has been run. It seems like the launching of the second Intend (my application starting) suppresses the first launch (Google translator starting). If I restore the 1-sec pause everything works as it should be.
Sure just do this:
Intent app_to_launch = getPackageManager().getLaunchIntentForPackage("com.package.of.background.app");
if (app_to_launch != null) {
startActivity(app_to_launch);
}
Intent app_to_launch = getPackageManager().getLaunchIntentForPackage("com.package.of.forground.app");
if (app_to_launch != null) {
startActivity(app_to_launch);
}
(The second app launch doesn't need to use the package manager, you can use anyone of starting an Activity, i.e. if you want to start your own app's internal activity, then just use the normal approach startActivity(new Intent(this, MyActivity.class))
That will start two apps, and the second one starting will move the first one second in the stack.
Note that apps don't really "run in the background" any app that isn't showing on the screen is usually in the paused state, (it may have started a background service to do some work though).

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();

How to keep a single Android instance of app in current running apps

I have an android application which has two activities let's say A and B where A is the launcher activity. For activity A I registered an intent-filter which opens application for a specific URL. I want to have always a single task for my application in current running apps. To solve this problem I tried different combinations for launchMode attributes:
A singleTop, B standard but when I access app via intent URL I have two applications in current running apps
A singleTask B standard but every time when I open the app, it starts with activity A even the apps was already opened with activity B (in this case I want to resume the app)
A singleTask B singleTask the behavior is like A singleTask B standard
I want that my app to have the same behavior like for example gmail, always to have maximum one instance in current running apps, when I open it and there is already an instance in background, to resume it and if I open the app via intent filter I want to process the intent and provide content accordingly (for example gmail is in background and I receive a new mail notification after I click it, the android keep one instance for gmail and displays the new mail).
I use Samsung Galaxy Note 4 with Android 6.0
Problem solved
I found a solution for my problem, maybe it is not the best but it works. Firstly I added a new Activity to catch the intent filter which has launchMode = "SingleTask" and in its onCreate method I posted an event to kill all existing activities.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ("android.intent.action.VIEW".equals(getIntent().getAction())) {
// This Activity is started using an external app (eg: Gmail)
// So start the app from the beginning
// (redirecting to Activity A)
Intent mainIntent = getIntent(); // Copy the Intent used to launch me
// Launch the real root Activity (launch Intent)
mainIntent.setClass(this, StartActivity.class);
// Post an event to kill all existing activities
// To do this i use Guava
PubSub.getInstance().post(new KillActivityEvent());
startActivity(mainIntent);
finish();
} else {
// The activity wasn't started by an external app
finish();
}
}

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

App Restarts on bringing it to foreground from different launch sources

Hi I am stuck with this issue.
I have my app which has 3 activities:
SplashScreenActivity, LoginScreenActivity, ViewPagerActivity(which houses 3 fragments).
When I put the apk in the mobile sdcard and install and open using the packagemanager. My App starts up just fine.
Issue - But, now if I press the Home Button and again launch the app from the Apps drawer/Homescreen. The App seems to relaunch and I have to go through the entire flow of Splash and LoginScreen.
This issue does not occur if I launch the App the first time itself from the Apps drawer itself./If I long press the Home Button and select the App from recent apps list the app is resumed properly as well.
For Reference I launch activities using these flags
Splash->Login
Intent intent=new Intent(SplashScreen.this, LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
LoginActivity->ViewPagerActivity
Intent intent = new Intent(context, ViewPagerActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Home screen icon launches whatever activity you have declared as the MAIN ... LAUNCHER activity in your manifest. Generally, the launch activity in manifest should be the main activity of your app. From there you can invoke splash screens and login activities when needed.
remove these flags or the complete line of code
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
This seems to be an issue when launching with package manager.
https://code.google.com/p/android/issues/detail?id=2373
if (!isTaskRoot()) {
Intent intent = getIntent();
String action = intent.getAction();
if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && action != null && action.equals(Intent.ACTION_MAIN)) {
finish();
return;
}
}

Categories

Resources