Different behavior while launch Android app on ICS and Gingerbread - android

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.

Related

Prevent app from returning to BootActivity when Home is pressed on the navigation bar

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.

Android App opens second instance when "launched" after back button pressed

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

Android application doesn't appear on recent apps list

i'm deploying an android application (Android 4.0.3) and when I press the home screen the application doesn't stay on the recent apps list. I've noticed that when I restart the application, it starts on the last screen i've navigated before press the home button. Anyone can help? Here is me Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET" />
<application android:icon="#drawable/logo_home"
android:allowBackup="true">
<activity
android:name="com.test.activity.CommunicatorActivity"
android:launchMode="singleTop"
android:label="" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.test.activity.InputActivity"
android:windowSoftInputMode="adjustPan|stateHidden">
</activity>
<activity android:name="com.test.activity.SettingsActivity"
android:windowSoftInputMode="adjustPan|stateHidden">
</activity>
<activity android:name="com.test.activity.OutputActivity"
android:windowSoftInputMode="adjustPan|stateHidden">
</activity>
</application>
</manifest>
I'm going to go out on a limb and suggest that this is your problem:
android:label=""
In your root activity CommunicatorActivity you've set the label to be an empty string. This label is used to identify your app in the list of available apps and because it is empty, this may be causing it not to show up in the list of recent apps.
Remove android:excludeFromRecents="true" from your manifest
You should use
android:label="your app_name"
2 things
Ok, there isn't something obvious in your manifest that should prevent it from appearing in your recents list.
1. Wrong identification
It's not always straight forward to tell which task contains your activity, using the GUI.
One of the possibilities is that an activity from some other app is on top of your task making you believe that your activity isn't there. Since, clearly it resumes to the last screen.
I suggest you to remove the single top temporarily and check the same.
2. Launcher modification
Has the launcher you use the standard launcher? Has the launcher or some other app modified the presentation or behavior of the recents screen (unlikely but worth a check)
For this, I suggest using a stock launcher or try the same app on a different phone or an emulator.
What would help is steps you followed and some screen shots of recents
screen and also of your app
I had the same problem.
The reason is that I had set android:label="" in manifest.xml.
I hadn't set android:launchMode="singleTop".
I declared on purpose android:label="" because I didn't find any better way to remove the title of the action bar from my main activity.
I turned android:label="app_name" again and everything now is OK; the app appears again in recent app list when pressing the home screen and doesn't start on the last navigated screen when restarting it.

I click HOME button and the program minimizes; but when I press the app icon, the program is launched twice

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

Help with first Android Activity

When my app first opens my first activity that is presented to the user can vary based on configuration options. I only know how to hard code the first activity that runs when the app is running by adding something like this in the Manifest
<activity android:label="#string/app_name" android:name=".MyFirstActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Where MyFirstActivity is the class name of the first activity class to be run. How can I dynamically select which activity to run first when the app is first launched rather than hard code it in the manifest?
Thanks!
Option #1: In onCreate() of MyFirstActivity, call startActivity() for the right activity, then finish().
Option #2: Define several activities with the LAUNCHER <intent-filter>, all but one disabled. On first run (or as needed), enable the right activity and disable the others. Downside: may require a phone reboot to update the launcher, since not all home screen launchers will detect your change.
Option #3: Redesign your GUI such that this is not an issue.

Categories

Resources