I have two activities Activity A and Activity B that needs to communicate very frequently, if i will use startActivity on both activities then every times it will create new instances of them.
If i will use launchMode as singleTask then suppose from Activity A i call Activity B then if Activity B doesn't exist in the Task, new Instance of Activity B will be created in the Task and that will be on top of the Task, Now if i will call Activity A from B then Activity B will gets cleared from Task(singleTask makes A as the root Activity).Again calling B from A will create new Instance of B but i want if the the Activities exist in Task then it should not create new Instances.
How do i achieve this ?, Any help will be appreciated.
I don't know your use case why are you trying to have two activity instances at the same time which is against the design of android. You can also use Fragments within your activity to fulfill your purpose.
I think better option is to use observer pattern. Use broadcast receiver to send the message between activities.
Related
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.
I'm very new to Android programming (and Java for that matter) coming from an iOS background.
What I am trying to do, is pass a pointer to a Fragment from one Activity to another.
Basically, I have a starting activity called BeginActivity that handles a couple of Fragments for login and register screens. Once logged in, I load up the main activity of the app called TabsFragmentActivity using this code:
public void loggedIn() {
Intent intent = new Intent(this, TabsFragmentActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
this.startActivity(intent);
finish();
}
I'm using FLAG_ACTIVITY_CLEAR_TOP as I dont want the user to go back without actually logging out first.
Now the problem:
In BeginActivity I have a pointer to a fragment that holds the users data. I am using it like a singleton, that the first few view fragments can access from BeginActivity.
I need to pass this same object to the new TabsFragmentActivity before I call finish() on it.
How do I do this?
I know I can use putExtra() but I believe that is just for strings etc.. and not other Fragments.
Is there a way in the newly created TabsFragmentActivity that I can reference the BeginActivity to 'grab' the pointer?
Thanks
First of all, you should be sure about Fragments and Activity life cycle.
Fragments are designed to be reusable UI complex components. They look like activity, but you can reuse. So,you can have as many activities you need containing the same fragments, but not the same instances of these fragments.
If you just want to pass you user data for another activity you must use Bundle and putExtra(). Depending of the user data type can be necessary implements Serializable or Parcelable Interfaces, as #gheese said.
If you want to use the same UI appearence of your fragment on two or more activities, besides use Bundle and putExtra. Each activity that you want this behavior must contains a field whose is a Fragment and in the moment of starting this fragment you can use getActivity().getIntent().getExtra to get the user information and populate your fragment.
Basically you need to be able to pass your class via the intent, look at Serializable / Parcelable interfaces
This question has the answer you require
How to pass an object from one activity to another on Android
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)
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"/>
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.