CybeX
CybeX

Reputation: 2396

Resume activity from notification calling onCreate

I am trying to resume my activity from a notification, but it is recreating the activity and not resuming.

As all posts regarding this issue, mention creating a PendingIntent and setting this with the appropriate flags (as shown in code below), and adding the singleTop|singleInstance to the AndroidManifest.

Notification Code:

NotificationManager mNotifyMan = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
        mNotifyMan.cancelAll();
        Notification.Builder mNB = new Notification.Builder(this);
        mNB.setOngoing(true);
        Intent intent = new Intent(parentContext, main.class);
        intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
        PendingIntent pendingIntent = PendingIntent.getActivity(parentContext, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        mNB.setContentIntent(pendingIntent);
        mNB.setSmallIcon(R.drawable.ic_notify_icon);
        mNB.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_noti_large));
        mNB.setContentTitle(Title);
        mNB.setContentText(Message);
        mNB.setColor(0);
        mNotifyMan.notify(777, mNB.build());

I understand one must pass an intent with the current context.

When resuming an activity via the onPause() on onResume(), it resumes and restores the last activity state, i.e. a thread counter running has incremented values.

Problem:

When resuming from the notification (i.e. clicking on the notification), the activity is recreated. I have attempted to resolve this by

when onPause() is executed, sending the current activity state via base class to the notification, thus recreating the notification with the current state.

Here is my AndroidManifest:

<?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"
        android:launchMode="singleInstance">

        <activity android:name="com.cynetstudios.frequencyselector.main" android:configChanges="orientation|screenSize|keyboardHidden">
            <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 might be missing something trivial, any advice?

Upvotes: 1

Views: 2274

Answers (5)

Mehul
Mehul

Reputation: 9

Try this it may help you:

intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

and as @Alexander says put android:launchMode="singleTop" into activity tag

Upvotes: 0

空気嫁
空気嫁

Reputation: 186

Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setClass(this, main.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
startActivity(intent);

You can try this Intent. Such an intent is used by system app launcher, which will just show(no recreate) your activity if it is not killed.

Upvotes: 1

savepopulation
savepopulation

Reputation: 11921

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.

Upvotes: 4

ammcom
ammcom

Reputation: 1034

Try

Intent.FLAG_ACTIVITY_CLEAR_TOP

with

Intent.FLAG_ACTIVITY_NEW_TASK

Set them as flags for the intent

Upvotes: 0

Oleksandr
Oleksandr

Reputation: 1502

launchMode should be used in Activity section (not application)

Upvotes: 1

Related Questions