- Android Notification PendingIntent Extras null
- 6 Answers 6
- Activity re-creation intent extras are null
- android camera: onActivityResult() intent is null if it had extras
- 5 Answers 5
- CameraFragment.kt
- Invoke camera intent
- Handle camera result
- Intent extras null on configuration change
- 2 Answers 2
- Is a non-null object from intent?.extras? nullable?
- 3 Answers 3
Android Notification PendingIntent Extras null
I am trying to send information from notification to invoked activity, while from my activity I got null.
The code for notification is:
Code for get information from intent in main activity;
I have verified before the notification get notified, bundle is not null, and it has id in extras. While, when I tried to fetch it from intent, it’s gone. Help.
6 Answers 6
in PendingIntent use this flag PendingIntent.FLAG_UPDATE_CURRENT it’s work for me
For me, in addition to setting Intent.FLAG_ACTIVITY_SINGLE_TOP , I had to add a unique action to the intent:
.. without the setAction(..), Extras is null on the Intent received by my NotificationActivity.
I just got the answer, add line: resultIntent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
NOTICE: if you add it as resultIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP); It won’t work.
I also tried other flags like, «FLAG_ACTIVITY_NEW_TASK» and «FLAG_ACTIVITY_RESET_TASK_IF_NEEDED». neither works here.
When an activity is launched(when it is first started ) or relaunched(when it’s brought to the top of the stack) the getIntent().getExtra() won’t give null . But when the activity is present on the top of the stack then on starting that activity with the use of PendingIntent would not relaunch the activity( onCreate() won’t get called) instead onResume() would be called. And getIntent().getExtra() would return the value which is associated with the intent that started the activity(which is null ).
In order to update the intent do the following steps:
1). Set FLAG_ACTIVITY_SINGLE_TOP and FLAG_ACTIVITY_CLEAR_TOP flags in your intent.
2). Override onNewIntent() in the activity where getIntent().getExtra() is called. This method is called by FLAG_ACTIVITY_SINGLE_TOP
I was working in a local notifications plugin for Unity when I encountered the same problem -> I launched the app, scheduled the local notification, sent the app to background, local notification appeared and, when I clicked on it, app was resumed and getIntent().getExtras() was null .
In my case, the problem was I was trying to get the extras at the wrong intent. Try to print the intents with a simple toString, at the creation moment and when you get it, to ensure that the received is what you expect.
Also take a look to the onNewIntent method.
If you are using the implicit intent to build a PendingIntent, you may get this problem.
previous approach with the issue.
based on implicit Intent tried multiple approaches listed above not work for me.
here is the approach solved my problem. change the Intent from implicit one to an explicit one.
Источник
Activity re-creation intent extras are null
My app contains a MainActivity and uses full screen fragments to display content. I’m trying to achieve the following during app re-creation (When the app has been in background for a long time, gets killed by the system and then it’s brought to the foreground)
If the user is manually re-creating the app (by selecting the app from the recent’s apps list), the Main Activity should re-create and then the last fragment that was fullscreen should recreate. No problem here, this is the standard behaviour.
If the app is being re-created because the user touched a push notification, The Main Activity should re-create but the last fragment that was in fullscreen should not re-create. Instead a new Fragment should be created and displayed. Why? The push notification contains the info as to what kind of Fragment should be displayed
My approach relies on checking the intent extras that I put when building the notification that starts the Main Activity, That part of the code is not shown for brevity’s sake but I always put some extras in the intent, always
Now, if the app is not running at all and a notification arrives, I get the following output:
When the app is re-created manually by the user I get The following output:
When the app is re-created by a notification
This last case is not what I expected, somehow the notification intent extras are only available after calling super.onCreate()
Any ideas on how to achieve 1. and 2.?
Giant Edit: This is the notification code, as you can see, there is a splash screen activity that relays the intent to the Main Activity
Источник
android camera: onActivityResult() intent is null if it had extras
After searching a lot in all the related issues at Stack Overflow and finding nothing, please try to help me.
I created an intent for capture a picture. Then I saw different behavior at onActivityResult() : if I don’t put any extra in the Intent (for small pics) the Intent in onActivityResult is ok, but when I put extras in the intent for writing the pic to a file, the intent in onActivityResult is null !
The Intent creation:
Why is it null, and how can I solve it?
5 Answers 5
It happens the same to me, if you are providing MediaStore.EXTRA_OUTPUT , then the intent is null, but you will have the photo in the file you provided ( Uri.fromFile(f) ).
If you don’t specify MediaStore.EXTRA_OUTPUT then you will have an intent which contains the uri from the file where the camera has saved the photo.
Don’t know if it as a bug, but it works that way.
EDIT: So in onActivityResult() you no longer need to check for data if null. The following worked with me:
A sample written in Kotlin. You create a Uri for camera app, CameraFragment holds it until camera returns from saving your picture and gives it back to you in onActivityResult as you would expect.
CameraFragment.kt
Acts as an intermediary between consumer and camera app. Takes Uri as input and returns it in data Intent .
Invoke camera intent
this is a fragment in your app which will trigger the camera. RC_CAMERA is your request code for this action.
Handle camera result
Where did you create the f for the Uri.fromFile(f) ?
It must be a valid File object. Try to create it before the EXTRA_OUTPUT line.
Try with something like this:
When we will capture the image from Camera in android then Uri or data.getdata() comes null. we have two solutions to resolve this issue.
- We can got the Uri path from the Bitmap Image
- We can got the Uri path from cursor.
I will implement all methods here, Please carefully watch and read these:-
First i will tell how to get Uri from Bitmap Image: Complete code is :
First we will capture image through Intent that will same for both methods so this code i will write one time only here :
Now we will Implement OnActivityResult :-(This will be same for both above 2 methods):-
\now we will create all above method to create Uri from Image and Cursor methods via classes:
Источник
Intent extras null on configuration change
I created a layout that displays on a SurfaceView and I can get the setDataSource by using Bundle extras = getIntent().getExtras() .
Everything works fine until I try to set the landscape layout from land\layout.xml .
The null pointer exception is on line
urlLink = «rtsp://» + newString.trim().substring(2);
which gets the value from
Bundle extras = getIntent().getExtras();
PS I would prefer not using android:configChanges=»orientation» as I’m trying to make the layout have different height/width value
EDIT
After adding these code thanks to cricket_007
I got this error instead
which point to these lines of codes
Just in case this is needed,these are the codes I use to pass the String from the previous class
2 Answers 2
Not always — if savedInstanceState is not null, then newString is the value of savedInstanceState.getSerializable(«urlAddress»); , which could possibly return null.
Alternatively, getIntent().getExtras() is null, therefore you hit
Which will definitely cause an error.
In either case, you can catch the error by using this
And, then to address the problem, you might have to implement onSaveInstanceState to put the url string into that savedInstanceState Bundle. But, you should be using putString and getString , probably, instead of put / get — Serializable . That way you avoid the cast.
In order to find where the variable is getting null, it’s just a matter of logging and debugging appropriately.
Approaches to saving your data between orientation changes can be found at Handling Runtime Changes
Источник
Is a non-null object from intent?.extras? nullable?
Lets assume, there is a non-null enum MainFlag object ( like enum class MainFlag < PARAM1, PARAM2 >) in an Activity by lateinit var :
Furtheron, I get that later in the onCreate() like:
and I used to use this flag in that activity on several places with null-checks
but then Android Studio complains:
Unnecessary safe call on a non-null receiver of type MainActivity.Companion.MainFlag
I am not sure using it without null checks, because if intent?.extras? fails, flag will not be set, thus null?
3 Answers 3
yes it can be null . You are getting the warning because you are down casting to a not nullable type
First of all you have to declare the variable as
then you can set the value of flag like this
Now the flag variable is nullable . So you need to check for nullable everywhere in the code like this:
If you are sure that intent?.extras?.getSerializable(ARG_FLAG) will never be null (and also none of the intermediate objects: intent and intent.extras ), then you can keep everything as is and just use flag.let < . instead of flag?.let < . .
If there is a chance that any of those is null , then you have to use a MainFlag? -type for your flag, e.g.:
. or risk a TypeCastException when keeping as MainFlag .
Источник