The application(target API level must be 7th) has FragmentActivity which analyzes at onCreate the fragment key passed as an extra.
Now what is needed is to reorder to front the activity that is already created with the given fragment key.
Let’s say the FragmentActivity with different fragment keys are FA1, FA2 and FA3 – each is the same activity Class instance with different fragments.
Now in the stack FA1 > FA2 > FA3 i want to use the intent rather than the back button to get to FA2, by default that gives:
FA1 > FA2 > FA3 > new FA2.
I’d like to get either FA1 > FA3 > FA2 as the FA3 might have some pending operations, FA1 > FA2 is not as good but definitely better than default.
If there were several activities I’d use the FLAG_ACTIVITY_REORDER_TO_FRONT flag for intents, but that does not work for this case.
FA1, FA2, FA3, etc. are all the instances of the same class MyFA, that’s why I’m not able to use the intent flag and the FragmentManager seems to be out of help until there’s a standard global fragments cache.
Milestone (currently working and to be improved) solution One thing I’ve learned today is activity-alias which allowed to make several aliases for the same activity with the different Intent extras used as id’s. Now with the REORDER_TO_FRONT flag it works as I wanted.
Solution feedback The solution has no low-level operations, I like a lot more than digging at the tasks or back-stacks. Now the drawback is that each of such activities needs a separate alias with the hardcoded path, I don’t really like it.
Requirements (bounty is here) Whoever comes with a decent optimization takes 150 300 cookies. Not bad ? Any other solid solution is also highly appreciated.
Currently I have like 10 aliases at application manifest, e.g.
<activity
android:name=".activity.FragmentActivity"
android:configChanges="orientation"
android:screenOrientation="portrait" >
<intent-filter>
<category android:name="android.intent.category.DEFAULT" />
<action android:name="com.company.name.intent.FragmentActivity" />
</intent-filter>
</activity>
<activity-alias
android:name="com.company.name.intent.FragmentActivity.FragmentedOne"
android:targetActivity=".activity.FragmentActivity" >
<intent-filter>
<action android:name="com.company.name.intent.FragmentActivity.FragmentedOne" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="fragment_key_extra"
android:value="FragmentOne" />
</activity-alias>
<activity-alias
android:name="com.company.name.intent.FragmentActivity.FragmentedTwo"
android:targetActivity=".activity.FragmentActivity" >
<intent-filter>
<action android:name="com.company.name.intent.FragmentActivity.FragmentedTwo" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<meta-data
android:name="fragment_key_extra"
android:value="FragmentTwo" />
</activity-alias>
And then the activities are reordered with
Intent intent = new Intent(
"com.company.name.intent.FragmentActivity.FragmentedOne");
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Based on your idea of using
activity-aliasto solve this issue, I wrote a Historian class that will do the following:startActivity()andactivityDestroyed()methods that will do some bookkeeping so the lookup table can be used to dynamically assign an alias to a running Activity based on the Intent.Here’s an example on how to use it:
in AndroidManifest.xml
within your Activity class
Instead of starting a new instance of your activity like this:
You do this:
So you still need to add a few
activity-aliasinto your manifest manually (to be used as a pool) and implement thematchIntent()method in your Activity (to help detect whether two Intents are equal to you) but the rest is handled dynamically by the Historian class.I haven’t tested the code exhaustively, but it seems to work fine on some simple tests that I did. The idea is actually very similar to my answer on the other question (just need to use
FLAG_ACTIVITY_CLEAR_TOPinstead ofFLAG_ACTIVITY_REORDER_TO_FRONTthere) but using theactivity-aliasinstead of the inner child classes make it much cleaner 🙂