I've actually stumbled over this issue a few times already and resolved it with different strategies all the time.
Now, I believe, I found a good "hack" to resolve my noHistory Problem when reusing the launcher-Activity in android.
The scenario
Your app starts with a login screen. On the first start you would usually let the user create an account with a password or PIN to secure whatever your app is presenting. Now at some point in the app, usually in the settings, the user can change his/her password or PIN. In a best world scenario, your code can be reused.
In this case, I want to reuse the initial "create password/PIN" activity in my settings and use it as "change password/PIN".
The activityForResult issue
What should happen after the login in your app when the users presses the back button? You don't want the user to go back to the login screen, you want the user to exit the app to the home or apps screen. To do so, you have to set the flag "noHistory=true" to your launcher activity. Since this is the first activity, this has to be done in the AndroidManifest.xml file (you can't do it programmatically).
<activity android:name=".login.PinActivity"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
Now, if you want to reuse this activity with activityForResult
, you will just fail. Your activity is set to noHistory in the manifest file so you will not get any result back in onActivityResult
after it is finished. If you don't need the result you can stop reading here and you probably didn't even start your activity with activityForResult
, so you're totally fine.
Let's move on to the point where you need to know about the result.
My solution
I believe I had at least one app where I tried to totally redesign my architecture to get rid of this problem, but in my last project I had a really good working PinActivity
that I definitely wanted to reuse. I ended up creating a new activity PinActivityInAppNoHistoryHack
that just extends the PinActivity
and is declared as a separate activity in the manifest file without the "noHistory=true" flag:
<activity android:name=".login.PinActivity"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".login.PinActivityInAppNoHistoryHack" />
The class is dead simple:
package de.thesmile.android.login;
public class PinActivityInAppNoHistoryHack extends PinActivity {
}
I don't know of any activity limit in apps so I believe this little trick is acceptable and saves a lot of (duplicate) code and also time.
For me it was a small 'hack' with a great impact. If you would do things different, let me know and tell me why and how you solved it.