Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open already opened activity instead of creating new one?

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?

like image 423
TFR Avatar asked Sep 03 '11 14:09

TFR


People also ask

How do you start an activity only once the app is opened for the first time?

It is important to check that the first activity which opens when the app is launched is MainActivity. java (The activity which we want to appear only once). For this, open the AndroidManifest. xml file and ensure that we have the intent-filter tag inside the activity tag that should appear just once.

How do I start an app from another activity?

If both application have the same signature (meaning that both APPS are yours and signed with the same key), you can call your other app activity as follows: Intent LaunchIntent = getActivity(). getPackageManager(). getLaunchIntentForPackage(CALC_PACKAGE_NAME); startActivity(LaunchIntent);

How do I use intent to launch a new activity?

The following code demonstrates how you can start another activity via an intent. # Start the activity connect to the # specified class Intent i = new Intent(this, ActivityTwo. class); startActivity(i);


2 Answers

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

like image 184
CommonsWare Avatar answered Nov 08 '22 07:11

CommonsWare


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

like image 34
weakwire Avatar answered Nov 08 '22 09:11

weakwire