Kevin
Kevin

Reputation: 23634

Re-using the activity

final Intent intent = new Intent(getActivity(), Home.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
break;

I am trying to re-use my activity like this, but the activity is not getting opened at all. Below is my Android-Manifest.xml code for the corresponding activity.

<activity android:label="Home" 
    android:configChanges="orientation|screenSize" android:name="com.test.Home">
    <intent-filter>
        <action android:name="android.intent.action.Home" />
        <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
</activity>

Is it a good practice to re-launch the existing activity. I don't want it to get re-used in all cases only some situations, so tried the above Intent code.

Upvotes: 0

Views: 77

Answers (1)

abhinavrathore019
abhinavrathore019

Reputation: 6635

Just add it to you manifest file where you have declared your activity

It will create only single instance of your activity. after first creation when ever you will call this activity its onResume function will be called instead of onCreate. hence you will be able to use the same activity without creating it again and again

android:launchMode= "singleInstance"

or You can try adding the Flag

i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)

it will bring back the already created activity to front instead of of creating new instance

open the acitivty

like this

Intent i= new Intent(myactivity1.this,myactivity2.class);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
startActivity(i);

Upvotes: 2

Related Questions