I am trying to get data from a parceleable of the next way:
First, create a Bundle of the following way:
final Bundle extras = new Bundle();
I set the data
extras.putParcelable(TelecomManager.EXTRA_INCOMING_CALL_EXTRAS, data);
And in the other activity I try to get the data of the next way:
public Connection onCreateIncomingConnection(PhoneAccountHandle phoneAccountHandle, ConnectionRequest connectionRequest) {
Log.e(TAG, "onCreateIncomingConnection");
final Bundle bundle = connectionRequest.getExtras();
final Data data = (Data) bundle.get(TelecomManager.EXTRA_INCOMING_CALL_EXTRAS);
But when I try to get the Data this is always null, but I check that this is not null when I set the data.
Any idea of why Data become to null?
Thanks
replace .get with .getParcelable
Related
I am trying to expose media items to other media apps that can browse my app's content through my MediaBrowserServiceCompat service.
In my onLoadChildren method I am constructing MediaBrowserCompat.MediaItem with a MediaDescriptionCompat that includes a Bundle that has some extras that I need to play the item.
public class Service extends MediaBrowserServiceCompat {
...
#Override
public void onLoadChildren(#NonNull String parentId, #NonNull Result<List<MediaBrowserCompat.MediaItem>> result) {
val bundle = Bundle().apply {
putString("extra", "some value")
}
MediaDescriptionCompat description = new MediaDescriptionCompat.Builder()
.setMediaId(mediaId)
.setExtras(bundle)
.setTitle("title")
.setSubtitle("subtitle")
.setIconUri(uri)
.build();
MediaBrowserCompat.MediaItem item = new MediaBrowserCompat.MediaItem(description, MediaBrowserCompat.MediaItem.FLAG_PLAYABLE);
val items = ArrayList<MediaBrowserCompat.MediaItem>()
items.add(item)
result.sendResult(items)
}
So in the onPlayFromMediaId(String mediaId, Bundle extras) callback that I get when the user has clicked on the item, I am getting the right mediaId but the extras is an empty bundle.
private class MediaSessionCallback extends MediaSessionCompat.Callback {
...
#Override
public void onPlayFromMediaId(String mediaId, Bundle extras) {
super.onPlayFromMediaId(mediaId, extras);
//here extras is empty
}
I am sure that the MediaItem has the extras bundle when sent in the Result<List<MediaBrowserCompat.MediaItem>> result in onLoadChildren but I am not sure why it is being returned empty. What can cause such an issue?
Thank you!
I dont think you are actually getting the bundle. you set the parameter Bundle extra but I dont think there is actually anything in that bundle
usually how I have done it in the past after I create the bundle
to retrieve it would do something like this
create a variable to store the received
val extra:String
then use that string variable to get the bundle you created
extra = bundle.getstring("extra")
which "extra" is matching your key for the bundle that you created up top
which you pretty much have only your not actually getting the string from the bundle that the .getstring("extra") would get
Have same issue and I can't found relation between MediaDescriptionCompat's extras and onPlayFromMediaId's extras. So "mediaId" - that is only information you got from MediaItem and you need put all your data for onPlayFromMediaId here.
I have a diagram as follows
Activity 1 --send data--> Service1
^
|
Open app--------------------
^
|
Activity 2------------------
It means the activity 1 will send the data to a Service 1. The service 1 is a running background service with return in onStartCommand() is START_STICKY. Currently, I am using putExtra() function to exchange the data. And in the service I will getExtra data as follows steps:
In Activity 1
Intent start_service = new Intent(getContext(), ConnectService.class);
start_service.putExtra("data", "123");
getContext().startService(start_service);
In onStartCommand() of Service 1
String data=null;
if(intent.hasExtra("data")){
Bundle bundle = intent.getExtras();
if(!bundle.getString("data").equals(null)){
data= bundle.getString("data");
}
}
else
data="0";
I used the hasExtra function to check whether data is set or not. In my purpose, the case 1 is that the Service 1 can receive data from the Activity 1 . In the case 2, if we do not start from the Activity 1, for example, when I clean all running app, the phone will be open the Service 1 automatically due to START_STICKY, the Service 1 will set data equal "0". Hence, my issue is from second case, the application is crash because it cannot check the Extra exist or not.
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.content.Intent.hasExtra(java.lang.String)' on a null object reference
How could I fix the issue? Thank all
String data=null;
if(getIntent()!=null && getIntent().getExtras()!=null){
Bundle bundle = getIntent().getExtras();
if(!bundle.getString("data").equals(null)){
data= bundle.getString("data");
}
}
else
data="0";
try this,notice the change in if condition
At this step intent might be null. Also your code results in a possible null value for data. Instead you could do something like the following:
String data = null;
if (intent != null) {
data = intent.getStringExtra("data");
}
if (data == null) {
data = "0";
}
Also you should not use equals() to check for null values. Always use == null instead.
I want to use the data in fragments which is present in the Adapter. I am using setArguments() to send the data from Adapter, and getArguments() to receive the data.
But when I debug it, I am getting a nullpointerException at getArguments.
This is how I am sending the data and receiving it.
In Adapter, to send the data.
ReversalFragment f1 = new ReversalFragment();
Bundle bundle = new Bundle();
String transId = item.getTxId();
bundle.putString("tId", transId);
f1.setArguments(bundle);
fragment class, to get the data.
Bundle arguments = getArguments();
if(arguments!=null) {
String transId = arguments.getString("tId");
if (transId != null ) {
txView.setText(transId);
}
}
Can Anyone help me out how to handle this exception and why is getArguments() null??
Thanks in advance.
use can setTag on a view in adapter and getTag in your Fragment.
there are in built methods you can pass objects in setTag();
#ExportedProperty
public Object getTag() {
throw new RuntimeException("Stub!");
}
public void setTag(Object tag) {
throw new RuntimeException("Stub!");
}
You should call the fragment class object in your Adapter class.Suppose your fragment class name is FragmentB then you should call FragmentB class Object because in your program, it is unable to find out your fragment where do you want to pass the data.
FragmentB f1 = new FragmentB();
Bundle bundle = new Bundle();
String transId = item.getTxId();
bundle.putString("tId", transId);
f1.setArguments(bundle);
You are not specifying the name of the Fragment clearly,
instead of Fragment f1 = new Fragment();
write Your_Fragment_Name f1 = new Your_Fragment_Name();
it will work then.
You should try the other way around. In your current approach you are creating an object for the fragment class in you adapter class and assigning the values to that object, but when you are in your Fragment class it creates a new instance and the values are actually null.
So you must try something like this:
put getArgument() and setArgument() methods in your Adapter class.
I am pretty sure you create the object for Adapter in Fragment class to set it into listview or anything, at that time set and get the values.
For example
in Fragment class:
BaseAdapter adapter = new Adapter(); //constructor called and values set
Bundle args = adapter.getArgument();
now you can have the exact arguments in you need without making any further method calls
Adapter class:
Bundle bundle;
public Adapter(){
bundle = new Bundle();
//put required values
}
public Bundle getArgument(){
return bundle;
}
note that bundle variable is global in Adapter class
Since you would be creating the adapter from your fragment means you have access to your adapter in your fragment.
You can create public String getString() in your adapter and after the adapter is initialized you can call this method in your fragment as adapter.getString()
This question already has answers here:
What is a "bundle" in an Android application
(12 answers)
Closed 7 years ago.
I am new to android application development, and can't understand what does bundle actually do for us.
Can anyone explain it for me?
I am new to android application development and can't understand what
does bundle actually do for us. Can anyone explain it for me?
In my own words you can image it like a MAP that stores primitive datatypes and objects as couple key-value
Bundle is most often used for passing data through various Activities. Provides putType() and getType() methods for storing and retrieving data from it.
Also Bundle as parameter of onCreate() Activity's life-cycle method can be used when you want to save data when device orientation is changed (in this case activity is destroyed and created again with non null parameter as Bundle).
More about Bundle at its methods you can read reference at developer.android.com where you should start and then make some demo applications to get experience.
Demonstration examples of usage:
Passing primitive datatypes through Activities:
Intent i = new Intent(ActivityContext, TargetActivity.class);
Bundle dataMap = new Bundle();
dataMap.putString("key", "value");
dataMap.putInt("key", 1);
i.putExtras(dataMap);
startActivity(i);
Passing List of values through Activities:
Bundle dataMap = new Bundle();
ArrayList<String> s = new ArrayList<String>();
s.add("Hello");
dataMap.putStringArrayList("key", s); // also Integer and CharSequence
i.putExtras(dataMap);
startActivity(i);
Passing Serialized objects through Activities:
public class Foo implements Serializable {
private static final long serialVersionUID = 1L;
private ArrayList<FooObject> foos;
public Foo(ArrayList<FooObject> foos) {
this.foos = foos;
}
public ArrayList<FooObject> getFoos() {
return this.foos;
}
}
public class FooObject implements Serializable {
private static final long serialVersionUID = 1L;
private int id;
public FooObject(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
Then:
Bundle dataMap = new Bundle();
ArrayList<FooObject> foos = new ArrayList<FooObject>();
foos.add(new FooObject(1));
dataMap.putSerializable("key", new Foo(foos));
Pass Parcelable objects through Activities:
There is much more code so here is article how to do it:
Parcel data to pass between Activities using Parcelable classes
How to retrieve data in target Activity:
There is one magic method: getIntent() that returns Intent (if there are any data also with extended data) that started Activity from there method is called.
So:
Bundle dataFromIntent = getIntent().getExtras();
if (dataFromIntent != null) {
String stringValue = dataFromIntent.getString("key");
int intValue = dataFromIntent.getInt("key");
Foo fooObject = (Foo) dataFromIntent.getSerializable("key");
// getSerializble returns Serializable so we need to cast to appropriate object.
ArrayList<String> stringArray = dataFromIntent.getStringArrayList("key");
}
Usage of Bundle as parameter of onCreate() method:
You are storing data in onSaveInstanceState() method as below:
#Override
public void onSaveInstanceState(Bundle map) {
map.putString("key", "value");
map.putInt("key", 1);
}
And restore them in onCreate() method (in this case is Bundle as parameter not null) as below:
#Override
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState != null) {
String stringValue = savedInstanceState.getString("key");
int intValue = savedInstanceState.getString("key");
}
...
}
Note: You can restore data also in onRestoreInstanceState() method but it's not common (its called after onStart() method and onCreate() is called before).
In general english: "It is a collection of things, or a quantity of material, tied or wrapped up together."
same way in Android "It is a collection of keys and its values, which are used to store some sort of data in it."
Bundle is generally used for passing data between various component. Bundle class which can be retrieved from the intent via the getExtras() method.
You can also add data directly to the Bundle via the overloaded putExtra() methods of the Intent objects. Extras are key/value pairs, the key is always of type String. As value you can use the primitive data types.
The receiving component can access this information via the getAction() and getData() methods on the Intent object. This Intent object can be retrieved via the getIntent() method. And
the component which receives the intent can use the getIntent().getExtras() method call to get the extra data.
MainActivity
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putString(“Key“, myValue);
intent.putExtras(bundle);
startActivity(intent);
SecondActivity
Bundle bundle = getIntent().getExtras();
String myValue= bundle.getString(“key“);
A collection of things, or a quantity of material, tied or wrapped up together. it is the dictionary meaning...By the same Bundle is a collection of data. The data may be of any type i.e String, int,float , boolean and any serializable data. We can share& save the data of one Activity to another using the bundle Bundle.
Consider it as a Bundle of data, used while passing data from one Activity to another.
The documentation defines it as
"A mapping from String values to various Parcelable types."
You can put data inside the Bundle and then pass this Bundle across several activities. This is handy because you don't need to pass individual data. You put all the data in the Bundle and then just pass the Bundle, instead of sending the data individually.
It's literally a bundle of things; information: You put stuff in there (Strings, Integers, etc), and you pass them as a single parameter (the bundle) when use an intent for instance.
Then your target (activity) can get them out again and read the ID, mode, setting etc.
A mapping from String values to various Parcelable types.Click here
Example:
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.extras.putString(key, value);
mIntent.putExtras(mBundle);
Send value from one activity to another activity.
cancel(getIntent().getExtras().getInt(“notificationID”));
why we use of dot operator in between these methods? as cancel(int) method takes only one integer parameter.it has 3 methods as parametr.....what exactly the code will do..?
This is a short way to write:
Intent intent = getIntent();
Bundle bundle = intent.getExtras(); // or getIntent().getExtras();
int i = bundle.getInt(“notificationID”); // or getIntent().getExtras().getInt(“notificationID”);
cancel(i); // or cancel(getIntent().getExtras().getInt(“notificationID”));
What you do is to invoke methods on the return value of each method.
You should try going through the concepts of object oriented programming first.
To answer your question, getIntent() returns an object of type intent. We call the getExtras() on the Intent object which returns an object of type Bundle. Then we call getInt() on the Bundle object to finally get the int we want to pass to the cancel() method.
The statement is equivalent to :
Intent i = getIntent();
Bundle b = i.getExtras();
int id = b.getInt("notificationID");
cancel(id);
If we don't need any of the intermediate objects, we can write the whole thing in a single line.
Hope that helps.
cancel(getIntent().getExtras().getInt(“notificationID”));.. even here cancel is getting only 1 arguement... because.. getIntent()= returns an intent intent.getExtras = returns the values it stores if extras has some object then .getInt(“notificationID”) = returns an Int value.. So finally only thing remaining is integer...