In my PoC, I have some Activities, HomeActivity
, CreateActivity
, SearchActivity
, ResultsActivity
, ResultDetailsActivity
, and UpdateActivity
.
I have two main navigation paths: Create and Search.
Navigation for the Create path is as follows: HomeActivity
--> CreateActivity
-(on complete)-> HomeActivity
Navigation for Search is as follows: HomeActivity
--> SearchActivity
--> ResultsActivity
(ListActivity
) --> ResultDetailsActivity
--> UpdateActivity
-(on complete)-> ResultDetailsActivity
(with updated data).
Currently, navigation to a new Activity
is via startActivity(intent)
method. However, this is causing multiple instances of each Activity
to be opened.
I'm rather new to Android. Could someone please suggest how I could avoid this?
A task is a collection of activities that users interact with when trying to do something in your app. These activities are arranged in a stack—the back stack—in the order in which each activity is opened. For example, an email app might have one activity to show a list of new messages.
Task affinity lets you define which task an activity belongs to. By default, an activity has the same task affinity as its root activity. With task affinity, we can now separate activities into different tasks.
FLAG_ACTIVITY_NEW_TASK is equivalent to launchMode=singleTask and in there I read. However, if an instance of the activity already exists in a separate task, the system routes the intent to the existing instance through a call to its onNewIntent() method, rather than creating a new instance.
Go to: File -> Settings -> Appearance & Behavior -> System Settings -> Project Opening. Check [x] "Confirm window to open project in". Now open the other (2nd) project with File -> Open... etc. You will now be asked if you want to open a new window or replace what you already have.
In your android manifest, Add to your <activity>
tag the android:launchMode="singleTask"
For a full list, check the documentation of activity
In your manifest:
<activity android:name=".YourActivity" android:launchMode="singleTask" android:label="@string/app_name" />
Note: don't use singleton.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With