MapActivity instance management - android

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"/>

Related

how to destroy all the activites except the latest 3?

I have activities like A->B->C->D. How can I close the A activity if I have 4 activities on my stack? Also later when I open activity E i want B to be closed aswell, so I want to have C->D->E only.
There is no such as a direct way to manage activities number in stack. So far I know that stack is big as much as available memory.
Also consider LaunchMode and whether activities are in the same task or not.
So, you might implement your own Activity manager to finish un-wanted activities.
Here is briefly how I see the solution:
Create a model to store activity, its index in stack, date ..i.e. ActivityItem
Create an empty List of ActivityItem in your custom Application. To avoid memory leak, use WeakReference. create a public setter adActivity to add and manage activities
Better approach to create activity base class and reuse it as superClass wherever you want to manage count of running activities. instead of repeating same implementation for each different activity.
OnCreate in your base Activity call adActivity and pass current activity.
adActivity job is firstly clean the list of destroyed activities. thanks WeakReference. Then manually kill older activities before last 3 items in your list. It's not easy as it looks.. for example: SingleInstance and rotation will make it challenge to achieve this :-)
that is it.
Good luck,'.
When you start activity use
startActivityForResult(intent,code);//different code for all activity
and call
finishActivity(code);//which activity do you want to close?
You can call:
finish();
after:
startActivity(intent);
In every activity that you want to close, when you open another activity.

Get activity instance from called activity

I have an activity containing some fragments. One of those fragments calls another activity. In this new activity I need to have an instance of the first activity. GetParent() returns null so I don't know how I can acomplish this...
MainActivity --contains--> Fragment1 --startActivity()--> SecondaryActivity
Is there some way to get the calling activity on the SecondaryActivity?
I don't think there is a good way of doing so.
It's really a bad practise to handle activity references like that, since android wouldn't be able to gc them when needed (orientation changes, lack of memory).
The best way is to pass all the data you need with Intent extras (intent that you use to start activity), and, if you need SecondActivity to return something, use Fragment1.startActivityForResult() for starting activity, and when done, use SecondActivity.setResult() to return desired result, you will need to override onActivityResult() to get the result (there are plenty tutorials about this).
If you absolutely need to hold references to something, you can use your own instance of Application class (don't forget to declare it in the manifest) to hold data for you while application is running.

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 make a core of activity on android?

I am new to Android development. After learning from many tutorials I got many Activities and many Fragments. How can I make a core engine to check what Activity is running and what Fragment is showing on a container?
Assume that I have:
Acivity01, Activity02, ... , Activity10
Fragment01, Fragment02, ... , Fragment10
I want to make a class that filters the Activity where Activity is on runtime and what Fragment is embeded to that activity.
How can I do this?
If I understand you correctly, you may want to store some references within your Application class to an Activity and to Fragment instance(-s), which are currently in foreground (by this I mean that user can instantly interact with Activity/Fragment).
As for Activity
Create some Activity field in your Application class and getter/setter methods for it (e.g., setCurrentActivity(), getCurrentActivity()). Then call setCurrentActivity() from onResume() method for each of your Activity instances. Don't forget to call setCurrentActivity, supplying null reference to ir in order to properly handle a case, when there are no foreground activities, but application is stll working.
As for Fragment
The general idea is similar to the first item, but there can be more than one Fragment instance in foreground state at time. So you need to store something like List, where you add your resumed fragments and remove paused.
You may also want to implement something similar for dialogs, for example. Then use the same strategy. Hope it will help.

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.

Categories

Resources