Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – Why does my activity calls onCreate if opened from notification

Posted on November 11, 2022 By

Solution 1 :

You can use the singleTop launch mode to get the behavior you want.

From https://developer.android.com/guide/components/activities/tasks-and-back-stack:

If an instance of the activity already exists at the top of the current task, the system routes the intent to that instance through a call to its onNewIntent() method, rather than creating a new instance of the activity. The activity can be instantiated multiple times, each instance can belong to different tasks, and one task can have multiple instances (but only if the activity at the top of the back stack is not an existing instance of the activity).

For example, suppose a task’s back stack consists of root activity A with activities B, C, and D on top (the stack is A-B-C-D; D is on top). An intent arrives for an activity of type D. If D has the default "standard" launch mode, a new instance of the class is launched and the stack becomes A-B-C-D-D. However, if D’s launch mode is "singleTop", the existing instance of D receives the intent through onNewIntent(), because it’s at the top of the stack—the stack remains A-B-C-D. However, if an intent arrives for an activity of type B, then a new instance of B is added to the stack, even if its launch mode is "singleTop".

Solution 2 :

I changed my showNotification to this

public void showNotification(String Title, String info){
    int mNotificationId = 1;

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

    PendingIntent pendingIntent = PendingIntent.getActivity(MainActivity.this, 0, notificationIntent, 0);

    NotificationCompat.Builder builder =
            new NotificationCompat.Builder(MainActivity.this, "ALERTS")
            .setSmallIcon(R.drawable.notification_icon)
            .setContentTitle(Title)
            .setContentText(info)
            // Set the intent that will fire when the user taps the notification
            .setAutoCancel(true)
            .setContentIntent(pendingIntent);



     mNotificationManager.notify(mNotificationId, builder.build());
   
}

And also added singleTop in the Manifest like @Maurice Lam suggested.
Now it seems to be working correctly.

READ  [FIXED] java - switch case statements and run method from inside
Powered by Inline Related Posts

Problem :

I am building an Android app which should just open the app once its notifications are clicked.

    private void createNotificationChannel() {
        // Create the NotificationChannel, but only on API 26+ because
        // the NotificationChannel class is new and not in the support library
        // Gets an instance of the NotificationManager service
        mNotificationManager = NotificationManagerCompat.from(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            CharSequence name = getString(R.string.channel_name);
            String description = getString(R.string.channel_description);
            int importance = NotificationManager.IMPORTANCE_DEFAULT;
            NotificationChannel channel = new NotificationChannel("ALERTS", name, importance);
            channel.setDescription(description);
            // Register the channel with the system; you can't change the importance
            // or other notification behaviors after this
            mNotificationManager.createNotificationChannel(channel);
        }
    }

    public void showNotification(String Title, String info, int id){   
        // Create an Intent for the activity you want to start
        Intent resultIntent = new Intent(this, MainActivity.class);
        // Create the TaskStackBuilder and add the intent, which inflates the back stack
        TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
        stackBuilder.addNextIntentWithParentStack(resultIntent);
        // Get the PendingIntent containing the entire back stack
        PendingIntent resultPendingIntent =
                stackBuilder.getPendingIntent(0,
                        PendingIntent.FLAG_UPDATE_CURRENT);


        NotificationCompat.Builder builder =
                new NotificationCompat.Builder(MainActivity.this, "ALERTS")
                .setSmallIcon(R.drawable.notification_icon)
                .setContentTitle(Title)
                .setContentText(info)
                // Set the intent that will fire when the user taps the notification
                .setContentIntent(resultPendingIntent);


        mNotificationManager.notify(mNotificationId, builder.build());
    }

I followed this guide: https://developer.android.com/training/notify-user/navigation

The notifications display correctly and once I click them it takes me to MainActivity, but it restarts MainActivity. Why does this happen and how can I make it to just open it as if opened from onResume?

[EDIT] added my XML structure

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns_android="http://schemas.android.com/apk/res/android"
    package="com.company.example">
    
    <application
        android_allowBackup="true"
        android_icon="@mipmap/ic_launcher"
        android_label="@string/app_name"
        android_roundIcon="@mipmap/ic_launcher_round"
        android_supportsRtl="true"
        android_screenOrientation="sensorPortrait"
        android_theme="@style/AppTheme">
        <meta-data
            android_name="com.google.android.gms.ads.APPLICATION_ID"
            android_value="..."/>
        <activity
            android_name=".MainActivity"
            android_label="@string/app_name"
            android_windowSoftInputMode="adjustPan"
            android_launchMode="singleTop"
            android_exported="true">
            <intent-filter>
                <action android_name="android.intent.action.MAIN" />
                <category android_name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android_name=".Settings"
            android_label="Settings"
            android_exported="false"
            android_parentActivityName=".MainActivity"
            android_theme="@style/AppTheme.NoActionBar">
        </activity>
        <service
            android_name="com.company.example.DeviceService"
            android_permission="android.permission.BIND_MIDI_DEVICE_SERVICE"
            android_exported="false">
            <intent-filter>
                <action android_name="android.media.midi.MidiDeviceService" />
            </intent-filter>

            <meta-data
                android_name="android.media.midi.MidiDeviceService"
                android_resource="@xml/device_info" />
        </service>

    </application>

</manifest>

Comments

Comment posted by aWiseMan

I cant seem to get this to work. I added launchmode=”singleTop” to my mainAcitivity xml (which is my launch activity). I also added the override onNewIntent and let it print something but it never printed it and the activity is still calling onCreate upon opening from a notification.

READ  [FIXED] android - Listview ArrayAdapter custom item onClickListener working only first time
Powered by Inline Related Posts

Comment posted by stackoverflow.com/a/42678747/2921519

Interesting… from my research I think that is because you are using

Android Tags:android, java

Post navigation

Previous Post: [FIXED] React-native android build fails due to missing files in the gradle cache?
Next Post: Can I use Google storage instead of iCloud?

Related Posts

[FIXED] Starting Flutter With Android Studio problems Android
[FIXED] android – Flutter : Several variant outputs are configured to use the same file name Android
[FIXED] xamarin.android – master detial page in xamarin android app Android
[FIXED] android – how to get imported library version on Application Android
[FIXED] java – Can’t save captured image in folder using custom camera in android Android
[FIXED] Android Studio – pick a resource window not showing Images I stored in the drawable folder Android

Archives

  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can Vicks humidifier be used without filter?
  • What color is Spanish green?
  • How old is Jamie in The War That Saved My Life?
  • When should I use scalp massager for hair growth?
  • Can I put polyurethane over liming wax?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme