App Restarts on bringing it to foreground from different launch sources - android

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;
}
}

Related

How to resume an application from another application?

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

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

reopen app from background

When user press "Home Button" my app go to Background, but is still running. I need bring my app to front again. For example i've this code:
Context ctx=getApplicationContext();
Intent i =ctx.getPackageManager().getLaunchIntentForPackage("com.example.test");
ctx.startActivity(i);
but this code is to open app in different activity, there are a method or something to do this "correctly"?
You can simulate the "launching" of the app the same way that Android launches the app when the user selects it from the list of available apps. If the user starts an application that is already running, Android just brings the existing task to the foreground (which is what you want). Do it like this:
Intent intent = new Intent(context, SplashScreen.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // You need this if starting
// the activity from a service
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
(Source: https://stackoverflow.com/a/12075313/3529926)
You can set this flag to your intent
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
and set the launch mode of your activity to single task in manifest (add this is your activity tags):
android:launchMode="singleTask"

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

Issue with FLAG_ACTIVITY_NEW_TASK ,FLAG_ACTIVITY_CLEAR_TOP- Android

I have an application installed in my device. I am trying to launch this application A from the buttonclick of another application B using the following code:
Button buttonStart = (Button)findViewById(R.id.buttonStart);
buttonStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setClassName("co.abc.android.test",
"co.abc.android.test.Abc");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
Following is my issue:
I launch application A
I press home button
From application B's button onclick, I launch app A again
Press return button to exit application A, which was now launched
from app B
Eventhough I exit the application A on pressing return button, I am again taken to the main activity of app A, which I had launched initially.
On referring for this issue, I have read in many places that using Intent.FLAG_ACTIVITY_CLEAR_TOP would resolve this. But since I call app A's intent from a place in which I have no access to its context, it gives me the following error.
01-01 00:09:54.694: ERROR/AndroidRuntime(283): *** FATAL EXCEPTION IN SYSTEM PROCESS: WindowManagerPolicy
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:884)
at com.android.internal.policy.impl.LockScreen$DialerMethods.onTrigger(LockScreen.java:218)
at com.android.internal.widget.multiwaveview.Dialer$2.run(Dialer.java:366)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at com.android.server.wm.WindowManagerService$PolicyThread.run(WindowManagerService.java:752)
How can I resolve this issue, such that when I press the return button, I do not see the same activity(if launched before) again?
Any help is much appreciated.
Try adding inside onClick method:
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
The best way to launch another application is like this:
Intent intent = new Intent();
intent.setClassName("co.abc.android.test", "co.abc.android.test.Abc");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
This simulates what Android does when the user selects the application from the list of available apps.
Assuming that "co.abc.android.test.Abc" is the root activity of that application (ie: the one with intent-filter ACTION_MAIN/CATEGORY_LAUNCHER) this will either start the application (if it isn't already running) or just bring the application from the background to the foreground (if it is already running).

Categories

Resources