Android back button not working as intended after background - android

I have an activity A from which I start activity B.
If I press the back button it will go back to activity A, however if I press the home button or change to another app and come back to my app the back button closes the app instead, so I can no longer get back to activity A without restarting the app.
I use no flags when creating the intent.
Java:
Intent i = new Intent(this, InCallActivity.class);
startActivity(i);
XML:
<activity
android:name=".MainScreenActivity"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MANAGER" />
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="#xml/searchable" />
</activity>
<activity android:name=".InCallActivity"
android:screenOrientation="portrait"
android:launchMode="singleInstance"/>

Please try removing the attribute android:launchMode="singleInstance" in your activities, and check if it works.
Follow these links for more information: Activity Syntax and Launch Mode

Related

Open recent activity on app icon press , after pressing the home key of device

My Launcher class name is "SplashScreen". I have two Activities "MainActivity" and "MenuList". When user opens the app, SplashScreen Activity is started. Splash Screen Activity validates and starts MainActivity. User opens MenuList Activity by clicking on button in MainActivity Screen and clicks on home button in MenuList Activity. When user opens the app it should directly open MenuList Activity. I have made changes as mentioned in below link. It is working fine when user press home button in MainActivity. It is not working when user press home button in MenuList Activity. Please help me.
https://stackoverflow.com/a/20815679/1517280
ScreenFlow:
SplashScreen -> MainActivity -> MenuList
Manifest code:
<activity
android:name=".SplashScreenActivity"
android:clearTaskOnLaunch="true"
android:launchMode="singleTask"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:windowSoftInputMode="adjustResize|stateHidden"
android:screenOrientation="portrait" />
<activity
android:name=".MenuList"
android:windowSoftInputMode="adjustResize|stateHidden"
android:screenOrientation="portrait" />
Remove android:clearTaskOnLaunch="true" from SplashActivity's element.
In fact, this doesn't work for your MainActivity either - since this flag is set, every time you get back to your application, the task's stack is being cleared and SplashActivity is being launched.

Closing background Activity

when i'm on the MainActivity in my app then press the setting menu in the action bar a new fragment is opened..there are two problems here
1- when i'm on the setting fragment then i press the multi-tasking button i found two windows for my app not one..the MainActivity and the settings
2- if i press the setting window when i'm still on multi-tasking window then press the back button it should take me to MainActivity instead it closes the app.
So now what i want to do is
1- close the MainActivity from the background once i open settings
2- if i press multi-tasking button and choose settings window in my app then press back it takes me to the MainActivity not close the whole app.
here is how i defined my activities in manifest file
<activity android:name=".MainActivity"
android:launchMode="singleInstance"
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=".Settings"
android:label="#string/settings_title"
android:excludeFromRecents="true">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.android.bee.MainActivity"/>
</activity>
<receiver android:name=".Notifications"/>
</application>
hope the image helps visualizing it..thanks alot
Remove the android:launchMode="singleInstance" attribute.

After home button and returning, it does not return to previous activity

Activity A is a splash screen.
Activity B is a menu screen that is singleTask.
Activity C is a screen with noHistory that selects a photograph from the gallery with the built-in intent for choosing a photograph.
Activity D is a screen that manipulates photographs.
The problem that I have is that when I am in Activity D and select the home button, if I choose the application to be launched once more it returns me to Activity A to launch Activity B again instead of launching Activity D where I left off. If I use the recent apps however, it switches back to Activity D just fine as expected. So, there must be something slightly error prone in the way that I have it setup. But, I should expect it to return to Activity D in both cases so that the user can keep coming back to the Photo viewing screen (Activity D).
Thoughts?
<activity
android:name="ActivityA"
android:theme="#style/Theme.CustomDefaultStartup"
android:noHistory="true"
android:configChanges="keyboardHidden|orientation|screenSize"
android:screenOrientation="sensorLandscape"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="ActivityB"
android:theme="#style/Theme.CustomDefault"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="#xml/device_filter" />
</activity>
<activity
android:name="ActivityC"
android:theme="#style/Theme.CustomDefault"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name">
</activity>
<activity
android:name="ActivityD"
android:theme="#style/Theme.CustomDefault"
android:configChanges="orientation|keyboardHidden|screenSize"
android:label="#string/app_name">
</activity>
Apparently it was the android:launchMode="singleTask" on Activity A that was causing the problems. Once, the singleTask from the splash screen was removed, the behavior returned as was expected. Not sure where the singleTask for the splash screen came from, but it was probably an error left in place for a while in the application.
Once that was removed, Activity D no longer launched the splash screen and the following menu after a home button press and return to the application. While I do not follow this completely as to why this would cause it in the lifecycle of events, this resolved the issue for me.

Android launchMode="singleTask" and Intent-filters

So I have an Activity A that is defined in the AndroidManifest.xml as defined below:
<activity
android:name=".activity.A"
android:screenOrientation="landscape"
android:windowSoftInputMode="stateAlwaysHidden"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This Activity launches a welcome Screen which we will call Activity B.
If you launch the application the Welcome screen is displayed and then once the user is done with it Activity A comes back.
The issue I am having is when I push the "Home" button from the welcome screen I go back to the Home Screen as expected. Now when I reclick on the Application Icon the application won't launch. Instead both my Activity A & B get destroyed. If I click on the icon again then the application relaunches as expected.
Now if I'm on the welcome screen and push the back arrow and reclick on the App icon it launches the application as expected. I don't have to push it twice.
Unfortunately I have to use the launchMode="singleTask" since it is a requirement for integration with another team. I have read the Android API's for Tasks and Back Stacks numerous times. Any pointers or suggestions would be greatly appreciated.
I came across a blog indicating there is an undocumented bug with using singleTask and intent-filters together but didn't find any official documentation on this.
Thanks
EDIT
Launching Activity B like this:
Intent intent = new Intent(context, B.class);
startActivityForResult(intent, CONST_VAR);
I tried making two activities which launches ActivityB from Activity A. I see no such problem as described in the question. PFB my manifest. Also, when you say home button, is it Phone home button or your app specific home button. PFB my manifest
<activity
android:name="com.android.testsingletask.MainActivity"
android:launchMode="singleTask"
android:screenOrientation="landscape"
android:windowSoftInputMode="stateAlwaysHidden"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.android.testsingletask.WelcomeActivity"
android:screenOrientation="landscape"
android:windowSoftInputMode="stateAlwaysHidden"
android:label="#string/app_name" >
</activity>

How to make sure that the launcher activity always run when the app is launched from the home

I have 2 activities in my app. The A activity is the launcher one. When I run the app the very first time, the launcher activity runs, but when I press the home button and restart the app from there by clicking the app icon, I always get the B activity running.
I want to make sure that the activity A should always run when starting the app.
This is the manifest code:
<application
android:icon="#drawable/icon"
android:label="#string/app_name" >
<activity
android:name="com.velosys.smsManager.Activities.a"
android:clearTaskOnLaunch="true"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.velosys.smsManager.Activities.b" />
</application>
I have searched a lot for this but did not get any clue.
Maybe android:launchMode="singleInstance" on activity B will get it done?

Categories

Resources