How to make notification intent resume rather than making a new intent?
The idea is that people can navigate
away from this activity and quickly
access it again from any screen they
want by pulling down the drop down
menu and selecting it.
Please make this optional.
However, when the notification is
pressed it starts a new instance of
the activity.
That will happen by default.
What would i have to change to make it
see if the activity has not already
been destroyed and i can just call
that instance back(resume it) and
therefore not needing to load it again
and won't need to add another activity
to my stack.
Get rid of FLAG_ACTIVITY_NEW_TASK
. Add notifyIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
-- see this sample project.
Context context = getApplicationContext();
Please don't do this. Just use your Activity
as a Context
.
Notification always creates new activity instead of resuming previous activity
I've solved the problem. I needed to call the setSessionActivity() method on my media session, like so:
Intent intent = new Intent(context, MainActivity.class);
PendingIntent pendingIntent = PendingIntent.getActivity(context, MediaPlayerContract.REQUEST_CODE,
intent, PendingIntent.FLAG_UPDATE_CURRENT);
mediaSessionCompat.setSessionActivity(pendingIntent);
EDIT:
you can also create an intent by simply calling
setContentIntent(controller.getSessionActivity())
after calling setSessionActivity, where controller is your MediaSessionController.
How to make notification resume and not recreate activity?
Looks like the problem was caused by these lines in the notification-code (taken straight from Android's guide on notifications:
TaskStackBuilder stackBuilder = TaskStackBuilder.create(context);
stackBuilder.addParentStack(FieldAgent.class);
stackBuilder.addNextIntent(intent);
PendingIntent pendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
Replacing them with a regular pendingintent like this solved it:
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
An complete example
Intent intent = new Intent(getApplicationContext(), myactivity.class);
PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), (int)System.currentTimeMillis(), intent, 0);
Notification myNotification = new Notification.Builder(getApplicationContext())
.setContentTitle("Title")
.setContentText("Some text....")
.setSmallIcon(R.drawable.myicon)
.setContentIntent(pIntent)
.setAutoCancel(true).build();
NotificationManager notificationManager =
(NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notificationManager.notify(0, myNotification);
resuming an activity from a notification
I've solved this issue by changing the launchMode
of my activity to singleTask
in the androidManifest.xml file.
The default value for this property is standard
, which allows any number of instances to run.
"singleTask" and "singleInstance" activities can only begin a task. They are always at the root of the activity stack. Moreover, the device can hold only one instance of the activity at a time — only one such task. [...]
The "singleTask" and "singleInstance" modes also differ from each other in only one respect: A "singleTask" activity allows other activities to be part of its task. It's always at the root of its task, but other activities (necessarily "standard" and "singleTop" activities) can be launched into that task. A "singleInstance" activity, on the other hand, permits no other activities to be part of its task. It's the only activity in the task. If it starts another activity, that activity is assigned to a different task — as if FLAG_ACTIVITY_NEW_TASK was in the intent.
you can find a detailed explanation in the Android Developers' Guide
I hope this helps
Resume an activity when clicked on a notification
You need to set flags in your PendingIntent's ...like FLAG_UPDATE_CURRENT.
Here is all on it.
http://developer.android.com/reference/android/app/PendingIntent.html
Edit 1: I misunderstood the question.
Here are links to topics that had the same issue but are resolved:
resuming an activity from a notification
Notification Resume Activity
Intent to resume a previously paused activity (Called from a Notification)
Android: resume app from previous position
Please read the above answers for a full solution and let me know if it works.
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 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.
Related Topics
How to Dynamically Remove Items from Listview on a Button Click
Connecting 2 Emulator Instances in Android
Flutter Run Error:You Have Not Accepted the License Agreements
In Androidx.Fragment.App.Fragment,Setuservisiblehint()Is Deprecated,And Not Executed,Why
Get the Background Color of a Button in Android
Use Camera Flashlight in Android
How to Add Window -- Token Android.Os.Binderproxy Is Not Valid; Is Your Activity Running
Webview Link Click Open Default Browser
Failed Binder Transaction When Putting an Bitmap Dynamically in a Widget
How to Get My Android Device Country Code Without Using Gps
Redraw a Single Row in a Listview
The Android Emulator Is Not Starting, Showing "Invalid Command-Line Parameter"
Adt Requires 'Org.Eclipse.Wst.Sse.Core 0.0.0' But It Could Not Be Found
Show Icon in Actionbar/Toolbar with Appcompat-V7 21
Error:Failed to Open Zip File. Gradle's Dependency Cache May Be Corrupt
Get Context of Popupmenu Like Contextmenu
Realmchangelistener Does Not Get Called When Realm Gets Updated in Notificationlistenerservice
How Do Mpandroidchart Renderers Work and How to Write a Custom Renderer