Resume Application and Stack from Notification

Resume application and stack from notification

Just use the same intent filters as Android uses when it launches the app:

final Intent notificationIntent = new Intent(context, YourActivity.class);
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

As the Intent you created to open your Activity from the notification bar is the same as Android used for launching your app, the previously opened Activity will be shown instead of creating a new one.

Resume Application from a Notification

You can try making the Activity as a single task by adding android:launchMode="singleTask" to your AndroidManifest.xml. Something like this:

<activity
android:name=".YourActivity"
android:launchMode="singleTask">

Then you need to handle the new data via onNewIntent(), something like this:

public class YourActivity extends Activity {

@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
...
processExtraData();
}

@Override protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
processExtraData();
}

protected void processExtraData() {
Bundle extras = getIntent().getExtras();

...
// process the extra here.
}
}

Android: How to resume an App from a Notification?

you need to set your flags

 notification.flags = Notification.FLAG_ONGOING_EVENT | Notification.FLAG_NO_CLEAR;   
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

Also, if you never ever want there to be a duplicate activity give it this attribute in the manifest

android:launchMode="singleTask"

Resume activity from notification calling onCreate

You can set your Activity's launchMode SingleTask, SingleTop or SingleInstance depending on how you want your activity to act.

Later you can handle your activity's onNewIntent method if your activity is open when user clicks your active notification.

Also you need to define activity's launchMode in that atcitvity's part. I edited your xml.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cynetstudios.wifimanager">

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature android:name="android.hardware.wifi" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">

<activity android:name="com.cynetstudios.frequencyselector.main"
android:configChanges="orientation|screenSize|keyboardHidden"
android:launchMode="singleInstance">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="com.cynetstudios.frequencyselector.serviceWifiFreqManager" android:exported="false"/>
</application>

</manifest>

I hope this'll help you.

How to programmatically resume an Activity from a notification?

After some trial and error testing, I got it working using the following combination of methods

PackageManager pm = context.getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(context.getPackageName());
intent.setPackage(null);
return PendingIntent.getActivity(context, 0, intent, 0);enter code here

The key part here was intent.setPackage(null);

/**
* (Usually optional) Set an explicit application package name that limits
* the components this Intent will resolve to. If left to the default
* value of null, all components in all applications will considered.
* If non-null, the Intent can only match the components in the given
* application package.
*
* @param packageName The name of the application package to handle the
* intent, or null to allow any application package.
*
* @return Returns the same Intent object, for chaining multiple calls
* into a single statement.
*
* @see #getPackage
* @see #resolveActivity
*/

So apparently, pm.getLaunchIntentForPackage(context.getPackageName()); returns an Intent limited to the components in context.getPackageName() and setting the package explicitly to null is needed afterwards so that all components are considered.

I'm not completely sure about ^ though. FWIW adding it, solved the issue ¯\_(ツ)_/¯



Related Topics



Leave a reply



Submit