Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resume the Top Activity instead of starting the Launcher Activity

I have two activities in My application, one being launcher and the other is launched as a explicit call from the first.

Here My problem is when i go back to home screen by pressing home key from second activity and launch the application, again the first activity gets initiated even though the second activity is already in the background.

The first Activity is written to download the required assets for the application to work, once the assets are downloaded it triggers the second activity and calls finish for self.

Below is my manifest of the application.

<uses-sdk android:minSdkVersion="7" android:targetSdkVersion="14" />  <application android:icon="@drawable/icon" android:label="@string/app_name" android:theme="@android:style/Theme.NoTitleBar.Fullscreen">      <! Download the Required Assets if not found on SD Card -->     <activity android:name=".ContentDownload"         android:screenOrientation="landscape"         android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|locale"          android:launchMode="singleTask"         android:alwaysRetainTaskState="true">          <intent-filter>             <action android:name="android.intent.action.MAIN" />             <category android:name="android.intent.category.LAUNCHER" />         </intent-filter>          <intent-filter>             <action android:name="android.intent.action.SEARCH" />             <category android:name="android.intent.category.DEFAULT" />         </intent-filter>      </activity>      <activity android:name=".ActualAppActivity"          android:screenOrientation="landscape"          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize|locale"          android:launchMode="singleTask"         android:alwaysRetainTaskState="true"         />  <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS"/> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>  <uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>    <supports-screens android:smallScreens="false" android:normalScreens="true" android:largeScreens="true" android:anyDensity="true"/> 

Can Somebody please guide me on how to make the second activity gain the control directly instead of going through the first again if launcher is called and it is in the background.

Below is my onResult Call back method.

public void onResult(String assetPath, int result) {     if(result == RESULT_OK)     {         startActivity(new Intent(this, ActualAppActivity.class));         activity.destroyDownloadActvity();         finish();     }     else     {         finish();         java.lang.System.exit(0);     }     activity.destroyDownloadActvity();     activity = null; } 
like image 304
Sudhaker Avatar asked Jul 30 '12 12:07

Sudhaker


People also ask

What does launcher activity mean?

Launcher Activities are the activities that can be launched for a given intent. For example, when you press an app icon on the home screen, the StartActivity intent starts the activity you have specified as the launcher activity.

How does Android know which activity to run first?

Activities will very often need to support the CATEGORY_DEFAULT so that they can be found by Context. startActivity(). So, CATEGORY_DEFAULT can appear number of times. Android does not grab whichever one appears first in the manifest but it starts with activity having CATEGORY_LAUNCHER.

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.


2 Answers

Try using the following code in the onCreate method of the activity that is specified as the Launcher Activity in the Manifest, i.e. the ContentDownload activity from the original code posted in the question:

if ((getIntent().getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT) != 0) {   finish();   return; } 

This will finish your Launcher Activity before it is displayed by detecting that there is already a task running, and your app should instead resume to the last visible Activity.

See this page in the Android documentation regarding Android Manifest launchModes: http://developer.android.com/reference/android/R.styleable.html#AndroidManifestActivity_launchMode

like image 139
Jadent Avatar answered Sep 22 '22 04:09

Jadent


I use the following code in the LAUNCHER Activities of my apps to prevent the app from beeing started again when its still alive in the background and the icon is tapped

@Override protected void onCreate(final Bundle savedInstanceState) {     super.onCreate(savedInstanceState);      if (!isTaskRoot()) {         finish();         return;     }      // Rest of your onCreate stuff goes here } 

This just finishes the LAUNCHER Activity and resumes the last used one.

like image 42
Saenic Avatar answered Sep 22 '22 04:09

Saenic