user2999943 user2999943 - 1 month ago 7
Android Question

How to resume Android Activity programmatically from background

Situation:


  1. Let's say I have currently working launched application Activity A.

  2. After some time I am pressing "Home" button. Application A goes to background.

  3. At this time, I am starting to use another app B - youtube for example or etc.

  4. Something happens (doesn't matter what in this context, let's say timer finished calculating time) in the application A which currently is minimized to background.

  5. On the event occurrence, application A activity automatically resumes from background.



Question:

How to accomplish step 5? Basically I need to know how to resume application from background programmatically.

I tried to launch intent to "restart" my application activity but it didn't worked:

Intent intent = new Intent(context, MainActivity.class);
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
context.startActivity(intent);


My manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.taxti"
android:versionCode="43"
android:versionName="1.5.3" >

<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="21" />

<permission
android:name="com.example.taxti.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />

<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />

<uses-permission android:name="com.example.taxti.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="com.taxti.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="com.taxti.permission.C2D_MESSAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.EXPAND_STATUS_BAR" />
<uses-permission android:name="android.permission.CLEAR_APP_CACHE" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:hardwareAccelerated="true"
android:label="@string/app_name"
android:name="com.taxti.Globals"
android:theme="@style/AppTheme" >
<activity
android:name="com.taxti.InitialActivity"
android:label="@string/app_name"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="sensorLandscape"
android:theme="@style/MyTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.taxti.MainActivity"
android:label="@string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan"
android:screenOrientation="sensorLandscape"
android:theme="@style/MyTheme">
</activity>

<service android:name=".MainActivityForegroundService" />

<intent-filter>
<action android:name="android.net`enter code here`.conn.CONNECTIVITY_CHANGE" />
</intent-filter>

<intent-filter>
<action android:name="android.intent.action.CALL_PRIVILEGED" />
<category android:name="android.intent.category.DEFAULT" />
<action android:name="android.intent.action.DIAL" />
<action android:name="android.intent.action.CALL_BUTTON" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="tel" />
</intent-filter>

<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxx" />
<uses-library android:name="com.google.android.maps" />

<receiver
android:name=".GcmBroadcastReceiver"
android:permission="com.google.android.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="com.taxti" />
</intent-filter>

</receiver>

</application>

</manifest>

Answer

In order to bring your app to the foreground, you must call startActivity() from another context (either a Service or a BroadcastReceiver). Just calling startActivity() from within an Activity won't bring your app to the foreground.

You don't need the ACTION and CATEGORY in your Intent, but you do need to set Intent.FLAG_ACTIVITY_NEW_TASK.

Comments