android – How to open already opened activity instead of creating new one?-ThrowExceptions

Exception or error:

I have two activities with “navigation menu” which has items for launching Activity1 and Activity2.
For example we starts Activity2 from Activity1 and then we want open Activity1 by tap on “navigation menu”, but when we do this we get new instance of Activity1 instead of open еxisting instance.
How can i open instance of Activity1 if it already exists and create new instance if not?

How to solve:

Add FLAG_ACTIVITY_REORDER_TO_FRONT to your Intent you use with startActivity().

###

add android:launchMode="singleTop" to your activity in the Manifest.xml

<activity android:name=".myActivity" android:label="@string/app_name"
            android:launchMode="singleTop" />

Check this out about different launchModes
also mind this:

As shown in the table above, 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

###

Set the flag of the activity to singleTask and override the onNewIntent(Intent intent) to catch the new intent.

###

The most complete answer would be to use android:launchMode="singleTask" and depending on your functionality, override onNewIntent since it will be called if there is already an instance of the Activity with the new Intent passed to it.

<activity
            android:name=".MainActivity"
            android:launchMode="singleTask"/>

Why?

Based on the question.
There are two Activities, Activity1 & Activity2

We open Activity1 and then from Activity1 we open Activity2. Then, inside Activity2:

How can i open instance of Activity1 if it already exists and create new instance if not?

As stated in AndroidManifestActivity_launchMode for singleTask

If, when starting the activity, there is already a task running that starts with this activity, then instead of starting a new instance the current task is brought to the front. The existing instance will receive a call to Activity.onNewIntent() with the…

Furthermore, under the intent class, if you read about the singleTask launchMode it already uses Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT and manually setting an intent to it is not normally set by application code.

As stated in FLAG_ACTIVITY_BROUGHT_TO_FRONT

int FLAG_ACTIVITY_BROUGHT_TO_FRONT
This flag is not normally set by application code, but set for you by the system as described in the launchMode documentation for the singleTask mode.

Therefore, by using singleTask launchMode you ensure that there is only one instance of your application and you do not have to be adding the FLAG_ACTIVITY_BROUGHT_TO_FRONT flag to your intents in every activity which calls your Activity2 as suggested by CommonsWare.

Now, if we use the android:launchMode="singleTop" as weakwire suggested, the link he provided himself clearly states;

“singleTop”…In other circumstances — for example, if an existing instance of the “singleTop” activity is in the target task, but not at the top of the stack, or if it’s at the top of a stack, but not in the target task — a new instance would be created and pushed on the stack.

In other words, we would end up with multiple instances of Activity1 in some scenarios which is what we do not want.

As final say, if you are one of those who love editing answers that contribute nothing to the answer itself, go answer some real questions if you really want to increase your Stack Overflow Reputation.

Leave a Reply

Your email address will not be published. Required fields are marked *