how to start always at the last used activity? - android

I've an application with a lot of activities. When returning from desktop or lockscreen, the expected behaviour is, that the last used activity is shown again.
This works flawlessly if the application is started again from the normal desktop icon. But if I add it to the lockscreen "widgetlocker" and start it from there, there is always the main activity shown. And unfortunately, if I close the main activity later by returning via back button, I return to all other instances of this main activity.
It might be related to the fact that I'm returned to the lockscreen when closing the application directly after having opened it from the lockscreen.
But I'm quite puzzled why the behaviour is different if I open from lockscreen or open normally.
Any suggestions? How can I force the application to be ALWAYS return to the last used screen?
...
# main activity:
<activity android:name="StudyOptions"
android:launchMode="singleTask"
android:configChanges="keyboardHidden|orientation|locale">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="com.ankidroid.category.DECK" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="application/vnd.anki" />
</intent-filter>
</activity>
<activity android:name="DeckPicker"
android:label="DeckPicker"
android:configChanges="keyboardHidden|orientation|locale"/>
...

how about maintaining the activity changes like a state machine. Giving a state number for each activity and persist the current in the database. And recover the last state from the database on startup and display the corresponding activity.

Related

onCreate method of activity always called with launchmode "singleTask"

I am using Branch IO in my application and as per their documentation, I am using android:launchMode="singleTask" for my activity.
Here is some code snippet of my AndroidManifest.
<activity
android:name=".SplashScreen"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:windowSoftInputMode="stateAlwaysHidden|adjustPan">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<data
android:host="open"
android:scheme="com.package.name" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
<!-- Branch App Links (optional) -->
<intent-filter android:autoVerify="true">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="app.link"
android:scheme="https" />
<!-- <data android:scheme="https" android:host="example-alternate.app.link" /> -->
</intent-filter>
</activity>
Everything is working fine except when I press Home button while using app and tap on App/Launcher Icon, onCreate method of Splash screen activity is called which makes it looks like app is launched from beginning. However, if I press home button while using an app and open it from recent apps, onCreate is not called and everything works perfectly.
How do I make consistent app behavior when brought to foreground from recent apps and App/Launcher icon?
I tried removing singleTask launch mode which makes launch perfect from App/Launcher icon and recent apps but when tapped on branch IO link, the new instance of an app is created. I can understand that to overcome this problem only they are asking to put singleTask in launch mode.
I have checked for this scenario in many apps which are using deep links and they do not have this problem!
I must be doing something wrong which I cannot see.
Is something is missing or implementation is wrong?
Solution to this problem is moving to Single activity architecture.
As splash activity is singleTask, onStart method of it will be called each time when the app is brought to foreground from launcher icon. One has to write routing logic in onStart method, when another activity is started from here, app state will lose.
If a single activity is there, this problem will not be there. However one will have to deal with nasty fragment backstacks. Not an easy but correct approach.
For a quick workaround, one can use this code in onCreate method of activity with launchmode as singleTask.
if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {
return;
}
This might help to avoid unnessary execution of code written in onCreate. However this is a recommended solution as it doesn't solve rootcause of problem.

Android App name is not displayed in the multitasking screen

I've developed a simple NFC application that reads and displays the data(NDEF Records) present in the NFC tag. I modified the Manifest file so that whenever a tag gets tapped, my app gets opened.
Now the problem is when I manually open the app and check the multitasking screen, it displays my app name i.e NFCReader but when the app gets opened itself when a tag comes in contact with the device, my app gets opened but when I check the multitasking screen, it doesn't display my app name. Instead it displays NFC Service. What should I do to overcome this issue?
I assume that you have set your activity declaration, in your manifest, to something like this:
<activity
android:name=".packagename.MainActivity"
android:alwaysRetainTaskState="true"
android:launchMode="singleTask"
android:theme="#style/AppTheme.Main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data
android:host="domain.com"
android:scheme="http" />
</intent-filter>
</activity>
I think the answer to your question is the following line of code that you forgot to add:
android:launchMode="singleTask"
I invite you to read about launch modes here and here.
Please note: This answer is based on the use of Reader mode API, otherwise check this answer.
Hope this helps!

Application icon does not open last used activity

In some situations (I do not know exactly what causes this to happen),
if I start my app, open another (here AaaActivity) activity and then click the home button to get to the phone home view, and then click the app icon from there, it starts in the application home view, instead of starting in the last open activity.
Why is this and what do I need to do to make it return to the last used activity (AaaActivity)?
Here is an excerpt from the manifest:
<activity
android:name="x.y.app.android.SsssActivity"
android:theme="#style/NoTitleBarNoBackground">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="x.y.app.android.AaaActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:label="#string/activity_videoview"
android:launchMode="singleTop"
android:screenOrientation="sensorLandscape"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="x.y.app.android.AaaName" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Multiple tasks can be held in Android OS in the background at once. However, if the OS is running many background tasks at the same time, the system might begin destroying background activities in order to recover memory, causing the activity states to be lost. This is OS design and you can't prevent it. What you can do is saving your state so that you can retrieve it next time. Tasks in Android

How to return back to browser after it sends Intent to start my app without exiting from my app?

So I have such tricky thing declared in Manifest to be able to start my app from a browser:
<activity android:name=".BrowserIntentCallBackActivity"
android:noHistory="true" >
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="*"
android:scheme="trickyapp" />
</intent-filter>
</activity>
this workds ok and starts my BrowserIntentCallBackActivity as expected. If my app wasnt previously started then finishing the BrowserIntentCallBackActivity returns user back to browser and its fine.
However if my app was previuosly started and is running on background finishing the BrowserIntentCallBackActivity returns user to previuos activity of my app. But I want to send user back to browser where he/she clicked the very special link and from where my BrowserIntentCallBackActivity was launched by intent. And I dont want ot kill my running app.
How to achieve this?
Try setting the launch mode of your activity:
https://developer.android.com/guide/topics/manifest/activity-element.html#lmode
I believe the correct mode for your task will be "singleTask" or maybe "singleInstance", although I have limited experience with those so you will have to experiment to be sure.

Android - start the same activity after starting the app

My app consists of 2 activities:
<activity
android:name="com.domain.android.MainActivity"
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.domain.android.AboutActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
I open my app, navigate to second screen, then go to the homescreen. From there I launch the app again - and it starts the main activity. But the app is running, it is just in the background, but why it didn't open the second activity? I guess it is something connected with activity's category. But how to fix that? Thanks.
Welcome to the ever-growing list of developers who have been bitten by this Android bug. Please see Re-launch of Activity on Home button, but...only the first time for all the gory details. And please go to Google Code and star the issues. We need all the noise we can make to get these fixed.
Hold your home button it will show the running apps click on your app it will open the activity from where you left. even if you click on the application launcher it will open the activity from where you left.
your application will not be in the same state sometimes if android needs resources it might end that activity. open an application move to next screen and press the home button and launch again it will open from where you left do the same with 5 or 6 apps then try launching the 1st app it will not be in the same state it will launch from the launch activity but any app you come to home screen and launch immediately it will open from where you left. If the background app is not doing anything android might end it if it needs resource. correct me if i am wrong. additional information i will be happy to know.
One issue may be that you have 2 activities designated as the main activity using:
<action android:name="android.intent.action.MAIN" />
You will probably have two icons in your launcher for your app. Each one will launch a different activity. You might be launching the first one again and again by using the icon for the first activity. Try removing
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
from your AboutActivity activity declaration. This might fix your problem.

Categories

Resources