Short Version:
An application I have been developing opens a second instance when it is "launched" after the back button has been pressed.
Long Version:
When the application is launched from the "home screen" it works fine- if the application is sent to the background by pressing the back arrow button, by pressing the circle button, or the square button to change to a different app - and then the application is resumed through the multi-tasking menu it works perfectly - resuming exactly where it left off - not showing the splash screen again and most importantly not opening a second instance. However if the application is sent to the background and then launched again from the home screen a second instance of the application is created! The splash screen displays again and the main activity is displayed on the screen rather than the last activity that was on the users screen! This is obviously not the desired behavior and many solutions I have attempted do not work.
Manifest:
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme"
android:launchMode="singleInstance">
<activity
android:name=".MainActivity"
android:screenOrientation="portrait"
android:launchMode="singleInstance">
</activity>
<activity
android:name=".Settings_area"
android:screenOrientation="portrait" />
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="AIzaSyDieXTCaFoIL0kJ_IM4UMBSQL3sNn92AWM" />
<activity
android:name=".MapsActivity"
android:label="#string/title_activity_maps" />
<activity android:name=".Splash"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".aboutPageActivity" />
<activity android:name=".turnOffFromNotification"
android:noHistory="true"></activity>
</application>
Edit:
I have also attempted to replace launchMode in the application tag with singleTask and singleInstance.
However if the application is sent to the background and then launched again from the home screen a second instance of the application is created! The splash screen displays again and the main activity is displayed on the screen rather than the last activity that was on the users screen!
This is an expected behavior of Android. The back button (its default implementation) is intended for a user to notify the system that he/she is done with the current Activity letting Android to destroy the Activity's instance.
Note, such a behavior has nothing in common with the android:launchMode attribute.
This is obviously not the desired behavior and many solutions I have attempted do not work.
You can't change it. It's a matter of a user who decided to leave the Activity and OS destroying the Activity's instance no more needed.
The situation was resolved by removing android:launchMode="singleInstance"> from everywhere except the <application> tag. Apparently launch mode is best used when the goal is to create another task - which was not the desired behavior. Stock Android masked this behavior while the modified version of Android with Touchwiz did not.
It saves the current Instance...(it will not destroy it).
when you open app again that time it will reopen the same Instance.
import android.view.KeyEvent;
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
moveTaskToBack(true);
return true; // return
}
return false;
}
Related
I am building an Android application that launches 3rd party applications - both of which run in full screen.
When the 3rd party application is launched, if i swipe up from the bottom, i see the Android navigation bar (good) - however, if i then press the Android Home button on this nav bar, the 3rd party application exits (good), but my application is returned to the main boot activity and not the activity from which i launched the application from.
My application manifest contains..
<activity android:name=".boot.BootActivity"
android:configChanges="keyboardHidden|locale|layoutDirection"
android:excludeFromRecents="true"
android:label="#string/app_name"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:windowSoftInputMode="stateAlwaysHidden">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.HOME" />
</intent-filter>
</activity>
<activity android:name=".main.MainActivity"
android:configChanges="keyboardHidden|locale|layoutDirection"
android:label="#string/title_activity_main"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:windowSoftInputMode="stateAlwaysHidden" />
The BootActivity performs a bunch of pre-loading and model generation before waiting for the user to select a button which starts loading MainActivity which displays the menus and synopsis for the 3rd party apps.
When 3rd party is launched, we swipe up and press Home on the Android nav bar, the logcat is getting both "android.intent.action.MAIN" and "android.intent.category.HOME".
As mentioned above, this is causing the app to return to the boot activity - but i would like the 3rd party app to exit and return to the apps synopsis in MainActivity.
Can intent-filters be added, removed or temporarily disabled programmatically?
Generally using these special launch modes (singleTask) is a bad idea. If your BootActivity is launching HomeActivity, then this will not run in a new task, even though the launch mode would imply that, because these activities both have the same taskAffinity. If you really want to have them run in different tasks, then you need to set the taskAffinity of one or the other (or both) so that they are not the same. Read a bit about how taskAffinity works.
I have two applications installed on my device, application A and application B.
When a user launches application A, they press a button to launch application B.
I use the following code:
Intent intent = Global.CurrentContext.getPackageManager().getLaunchIntentForPackage("some.random.app");
Global.CurrentContext.startActivity(intent);
The first time the user does this, it works fine and works as expected, however, if the user then launches the phones task manager and swipes away application B. When the user then presses the button in application A. The activity is not created.
I believe this is something to do with the way that the task and the activity are being destroyed by android. My thought process is that when the user swipes to kill application B that the activity is destroyed, but the task remains. Then the second time they press the button in application A, that it's attaching to the old task and then not showing for the user.
Application A's activity manifest for the activity:
<activity
android:name="a.a.a.LogonActivity"
android:label="#string/app_name"
android:noHistory="true"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Light.NoTitleBar"
android:windowSoftInputMode="stateHidden"/>
Application B's activity manifest for the activity:
<activity
android:name=".activity.HomeActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:windowSoftInputMode="adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
When I set android:noHistory="false", it works. I think it is android:noHistory="true" killed applicationA's top activity, makes it doesn't work.
What is happening:
Currently when i click the app icon for the first time i am loading
SplashActivity and then loading ActAtomicGodDetailDesc
Now when i minimize the app -- > then go to home screen -- > then
click on the app icon again ActAtomicGodDetailDesc is displaying
instead of SplashActivity
What i want:
Everytime i want the app to fire SplashActivity when i click the app icon from the home screen
manifest
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:noHistory="true"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ActAtomicGodDetailDesc"
android:label="#string/app_name"
android:screenOrientation="portrait" >
</activity>
</application>
How to achieve this ? any ideas
Everytime i want the app to fire SplashActivity when i click the app icon from the home screen
I think you require to set android:clearTaskOnLaunch="true" on Splash Activity such that whenever you click on app icon then it will start as root activity.
android:clearTaskOnLaunch http://developer.android.com/guide/topics/manifest/activity-element.html#clear
Whether or not all activities will be removed from the task, except for the root activity, whenever it is re-launched from the home screen — "true" if the task is always stripped down to its root activity, and "false" if not. The default value is "false". This attribute is meaningful only for activities that start a new task (the root activity); it's ignored for all other activities in the task.
When the value is "true", every time users start the task again, they are brought to its root activity regardless of what they were last doing in the task and regardless of whether they used the Back or Home button to leave it. When the value is "false", the task may be cleared of activities in some situations (see the alwaysRetainTaskState attribute), but not always.
Suppose, for example, that someone launches activity P from the home screen, and from there goes to activity Q. The user next presses Home, and then returns to activity P. Normally, the user would see activity Q, since that is what they were last doing in P's task. However, if P set this flag to "true", all of the activities on top of it (Q in this case) were removed when the user pressed Home and the task went to the background. So the user sees only P when returning to the task.
If this attribute and allowTaskReparenting are both "true", any activities that can be re-parented are moved to the task they share an affinity with; the remaining activities are then dropped, as described above.
<activity
android:name=".SplashActivity"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:clearTaskOnLaunch="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
My application contains a main activity A and several other activities B, C, D, E that can be started only from the main activity A via the Menu.
For example if I'm in activity B and I click the Back button, then I always come back to the main activity A.
Considering again we are in activity B and I click the Home button. Then I have different behavior when clicking the launch icon of my app to return to the app :
On Gingerbread (tested on Emulator) this cause activity B to display and I'm able to go back to activity A be pressing the Back button.
On ICS the behavior is different, this cause a new activity A to start and if I click the back button in A then I come back to B. This not the expected behavior, the right one for me is the Gingerbread one.
Another example, if I'm in the main activity A and I click the Home button. Then I click the launch icon :
On Gingerbread this cause activity A to resume. So if I click on the back button I exist from the app.
On ICS this cause a new activity A to be created. So if I click on the back button I come back to the previous activity A, and I have to click again on A to exit.
Here is the manisfest.xml content :
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="10"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.android.vending.CHECK_LICENSE" />
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"/>
<application
android:icon="#drawable/icon"
android:label="#string/app_name">
<activity
android:name=".B"
android:configChanges="orientation"
android:screenOrientation="portrait">
</activity>
<activity
android:name=".C"
android:configChanges="orientation"
android:screenOrientation="landscape">
</activity>
<activity
android:name=".D"
android:configChanges="orientation"
android:screenOrientation="landscape">
</activity>
<activity
android:name=".E"
android:configChanges="orientation"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="com.perfexpert.intent.ACTIVITY_E" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".A"
android:configChanges="orientation"
android:screenOrientation="portrait"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
And the code used to start activities from the main activity :
startActivityForResult(new Intent(this, B.class), REQUEST_CODE_B);
Why do I have this different behavior on ICS ? How to get the Gingerbread behavior on ICS ?
According to the Android Developers site the expected is the following :
Suppose, for example, that the current task (Task A) has three activities in its stack—two under the current activity. The user presses the Home button, then starts a new application from the application launcher. When the Home screen appears, Task A goes into the background. When the new application starts, the system starts a task for that application (Task B) with its own stack of activities. After interacting with that application, the user returns Home again and selects the application that originally started Task A. Now, Task A comes to the foreground—all three activities in its stack are intact and the activity at the top of the stack resumes.
This is the behavior I get on my Emulator (Gingerbread) but not on my Nexus S (ICS).
Thanks
I'm going to assume that you started the app initially (the first time) from an IDE (like Eclipse or IntelliJ) or after installing it using the installer (from the market or browser or clicking on APK in file browser). If so, this is a known bug in Android (see http://code.google.com/p/android/issues/detail?id=26658 ). Many people have struggled for days chasing this problem :-(
A simple workaround for this problem can be found at http://code.google.com/p/android/issues/detail?id=2373#c21
To verify this is your problem, don't start it from the IDE or installer. Simply install the app and then go start it from the list of available applications.
The bug is there on all devices, on all versions of Android (at least up to ICS, haven't tested on JellyBean yet). It all works as it should in the emulator, so you cannot use emulator behaviour as an indication of real device behaviour.
As far as i can tell, there should be no different behavior on ICS here.
The only thing i can think of is the new developer option to destroy activities.
From what you are describing, it seems you have it turned on.
Immediately destroy activities
Tells the system to destroy an activity
as soon as it is stopped (as if Android had to reclaim memory). This
is very useful for testing the onSaveInstanceState(Bundle) /
onCreate(android.os.Bundle) code path, which would otherwise be
difficult to force. Choosing this option will probably reveal a number
of problems in your application due to not saving state. For more
information about saving an activity's state, see the Activities
document.
http://developer.android.com/guide/developing/debugging/debugging-devtools.html
Edit: but this doesn't explain your quote below.
On ICS the behavior is different, this cause a new activity A to start
and if I click the back button in A then I come back to B.
I have the following problem:
When I press the Android HOME key, I can see the "Desktop" and my app icon. Then I press my app icon and my application launches twice. I don't want open my app twice.
How my program works:
I have 4 Activities (A, B, C, D).
A - The Main Activity: It is the first to open. It opens the other activity that has a lot of buttons. It's like a Java's main() method. I show a SplashScreen and I call another Activity. Then I finish my activity "A".
B - The Menu Screen: In this activity, I have some buttons, like a menu. I have a configuration button, update button, and Login Button. When I click the login button, I finish this activity and open the Login Screen (Activity "C").
C - The Login Screen: The user writes the Login and Password. If the login is successful, I finish this activity and open the Activity "D".
D - The application main screen: It stays opened all the time and launches another Activities. I finish this when I want close my application.
P.S.: I tried change the launchMode flag (androidManifest.xml), but didn't work.
My AndroidManifest.xml bellow:
<application android:label="#string/app_name" android:icon="#drawable/icon" android:name="MyApplication">
<activity android:name="A"
android:label="#string/app_name"
android:configChanges="orientation">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="br.com.site.B" android:label="#string/app_name" />
<activity android:name="br.com.site.C" android:label="#string/app_name" />
<activity android:name="br.com.site.D" android:label="#string/app_name" />
</application>
And this is my Activity "A.java" source:
public class A extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
finish();
startActivity(new Intent(this, AtualizaDadosFrame.class));
}
}
I don't want open my app twice.
Thanks in advance!
I'm going to assume that you started the app initially (the first time) from an IDE (like Eclipse or IntelliJ). If so, this is a known bug in Android (see http://code.google.com/p/android/issues/detail?id=26658 ). Many people have struggled for days chasing this problem :-(
Please don't change the launchMode. This is not the correct way to solve this problem. The default (standard) launchMode is the one that works in most cases.
EDIT (Added link to workaround):
A simple workaround for this problem can be found at http://code.google.com/p/android/issues/detail?id=2373#c21
You should set the desired launch mode in your AndroidManifest.xml.
You can restrict this.....
Please go through below link.
Home key press behaviour