Activity alias enabling with single top - android

In my app there is an activity with alias: MyActivity and MyAliasActivity registered in the Manifest. The purpose of the alias is to provide extra intent filter. This feature can be enabled/disabled in the runtime by the user. The activity and alias has got singleTop modifier.
However, if I start my app with the component disabled (MyActivity on the top), then I enable it, and broadcast the event matching the intent filter, MyActivity is created one more time (by the alias)- I've got two of them in the backstack which is undesired.
Can it he handled in any way? I need to have only one activity in the backstack.

You should use singleTask instead of singleTop. singleTask will guarantee that you only have one instance of an Activity in the backStack. In case you start an Activity for the second time with singleTask launchMode, the onNewIntent will be ran instead of creating a new Activity. Another solution is to use singleInstance, this works pretty much the same as singleTask, keeping only one instance of Activity and running onNewIntent if it is already created.
For more information here is a good explanation of how it works:
https://inthecheesefactory.com/blog/understand-android-activity-launchmode/en
Google reference:
https://developer.android.com/guide/topics/manifest/activity-element.html

Related

android activity backstack management

My app shows some content (video,pdf,img, etc ) and within every content I can start another content. What I want is to have only "one back history".
For example, if my activity history is like this:
VideoActivityIns1->PdfActivityIns1->VideoActivityIns2
I need to go back from VideoActivityIns2 to PdfActivityIns1, but one step back is should be MainActivity of my app.
How can I do this? Any help would be appreciated
Each activity has activity lifecycle methods you can override to achieve the result you need. Thus, you can either launch Activity2 onResume() on Activity1 onPause(),
http://developer.android.com/training/basics/activity-lifecycle/index.html
or, invoke ActivityManager to detect and manage the other activities.
http://developer.android.com/reference/android/app/ActivityManager.html
You can also make use of intent resolution mechanism to assign several priorities to your activities and then setup intent filters in each activity so you can start activities with a given priority in your code. You can do this either in Java or XML (though I suggest Java). Have a look at the Intent class.

how to pass a callback in activity

I have two Activity: Activity1 and Activity2
Activity1 start Activity2 and I want to send a result from Activity2 to Activity1, but I can't use startActivityForResult() cause the lanuchmode of Activity1 is singelInstance. Are there any ways to send a callback from Activity1 to Activity2?(So far as I konw, one is send BroadCaseReceiver, the other is made a static param in Activity2)
Many thanks!
startActivityForResult not working properly with launchMode singleInstance
A similar question, it suggested using saved instance state and or saving information to a db/global storage.
onActivityResult do not fire if launch mode of activity is singleInstance
Proposes that you use a different type, replacing singleInstance with singleTask
You could extend the activity you want to launch and force its type to a different one for this scenario, leaving the original as SingleInstance.
It will not work to use broadcast to communicate between two Activities. Only one of the Activities will ever be active at a time. It does make sense to use it to communicate between an Activity and a Service, for example.
Generally you can use intent extras to pass information to the next Activity. I.e, using putExtra.
(java.lang.String, android.os.Bundle)

MapActivity instance management

I have a MapActivity subclass and I want to preserve the stack, but I can't keep multiple instances of a MapActivity in the same process. So I have come up with 2 schemes to achieve this:
Pass the state of the MapActivity along with any intents it fires and then let the activities that get switched to reconstruct the MapActivity by sending an intent that recreates the activity. Additionally, the MapActivity would be set so that intents only ever create a single instance of this activity at a time. This approach is flawed as there are multiple exit paths from this activity so all of them would need to be changed to support this.
Replace the MapActivity with a mock activity that does the recreation of the activity in it's onResume() method or something and then the activities you switch to can remain blissfully unaware of this issue. The problem with this approach is I am unaware about how I should go about creating this mock activity and also fire an intent to start the activity I want to switch to.
So my question is this is there a better way to do this and, if not, how would I go about doing option 2, if it is possible?
EDIT: One possible way to do option 2 is to make the mock activity a waypoint that starts the target activity for you in it's onCreate(). But then one just has to be careful that if the onCreate() gets called again because the activity is being reconstructed, that one doesn't start the target activity again. This can be done by checking that savedInstanceState is null.
You should use SingleInstance attribute in the manifest file, this will bring the earlier launched instance to the top of the backstack
<activity android:launchMode="singleInstance"/>

Finish() ing duplicate activities

If you have several activities onPause() is there a way to finish a specific activity?
edit: so for example, imagine on start activity 1 is called. Then activity 1 uses an intent to go to activity 2. then an update is made to the database and calls activity 1_new again so that it displays the updated database. At that point i want to get rid of the old activity 1.
It depends on what you want to do. You'll need to look at the AndroidManifest.xml spec for activity calls stacks.
Specifically android:launchMode
<activity android:launchMode="singleTop">
Careful though, launchModes are very tricky and can get you into trouble since it also depends on how the activity is launched from the Intent itself.
singleTop will essentially keep only 1 instance of that activity in the stack.
From the Docs:
If an instance of the activity already
exists at the top of the target task,
the system routes the intent to that
instance through a call to its
onNewIntent() method, rather than
creating a new instance of the
activity.
What I ended up doing here was calling startActivityForResult in the first activity. That way I was able to redisplay updated information from the second activity.

Can I gain some efficiency by declaring appropriate members "static" in Android Activity

If an Activity is a singleton in practice, I think I can gain some efficiency by declaring appropriate members "static", with zero risk. Yes?
The Android documentation says -
there's never more than one instance
of a "singleTask" or "singleInstance"
activity, so that instance is expected
to handle all new intents.
This means you can use static members.
Besides, a standard or singleTop should have thread-safe static members only. Suppose the current activity stack is A-B-C-D. If the arriving intent is for an activity of type B which is in "standard" or "singleTop" mode. A new instance of B would be launched as (since B is not at the top of the stack), so the resulting stack would be A-B-C-D-B.
One thing please DO NOT use singleTask or singleInstance for this purpose. The activity launch flags are there to control how activity stacks behave. They have visible impact on the user interaction with your activity (making it non-standard). Those modes are intended to be used when you want that kind of user interaction, they should NOT be used to change the implementation details of your app.
No. The same Activity can be started multiple times in the same process. For example, you can try starting an Activity from itself, when clicking a button.
Yes, an Activity can be a "singleton" if you ensure that an instance of Activity A isn't started while another instance of Activity A is in the activity stack (an instance of Activity A could technically start another instance of itself).
Please see the activity property launchMode at android.developer page.
Quote: "standard is the default mode and is appropriate for most types of activities. SingleTop is also a common and useful launch mode for many types of activities. The other modes — singleTask and singleInstance — are not appropriate for most applications, since they result in an interaction model that is likely to be unfamiliar to users and is very different from most other applications."

Categories

Resources