Android: How to Pass Parcelable Object to Intent and Use Getparcelable Method of Bundle

Android: How to pass Parcelable object to intent and use getParcelable method of bundle?

Intent provides bunch of overloading putExtra() methods.

Suppose you have a class Foo implements Parcelable properly, to put it into Intent in an Activity:

Intent intent = new Intent(getBaseContext(), NextActivity.class);
Foo foo = new Foo();
intent.putExtra("foo ", foo);
startActivity(intent);

To get it from intent in another activity:

Foo foo = getIntent().getExtras().getParcelable("foo");

Hope this helps.

Parcelable object passing from android activity to fragment

Try this

First, change some small things.

MainActivity.java

// 1. Parse the object to the fragment as a bundle;
ContentMainFragment contentMainFragment = new ContentMainFragment();

Bundle bundle = new Bundle();
bundle.putParcelable("SampleObject", currentObject);
contentMainFragment.setArguments(bundle);

// 2. Commit the fragment.
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, contentMainFragment).commit();

ContentMainFragment.java

// 1. Get the object in onCreate();
if (getArguments() != null) {
link = getArguments().getParcelable("SampleObject").getLink();
}

Second, it doesn't seem there is something wrong with your approach, then double check if you are parsing a valid object (currentObject) in the Activity.

How to send an object from one Android Activity to another using Intents?

the most easiest solution i found is..
to create a class with static data members with getters setters.

set from one activity and get from another activity that object.

activity A

mytestclass.staticfunctionSet("","",""..etc.);

activity b

mytestclass obj= mytestclass.staticfunctionGet();

How to pass data between activity using parcelable in android studio

change itemviewclick like this

itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(context, DetailActivity.class);
//addthis i.putExtra(DetailActivity.MOVIE, entListMovie.get(getPosition()));
context.startActivity(i);
}
});

and in the detail make like this

add this

public static final String MOVIE = "movie";

in method onCreate() add this

YourList yourList = getIntent().getParcelableExtra(MOVIE);

after that, just set the data

textview.setText(yourList.getBlaBla());

How to pass data class as Parcelable in a bundle?

Serializable while using reflection and causing a bit more garbage collection, is easier to implement.

I find it easiest to use GSON.
https://github.com/google/gson

First, add it to your data class like this:

data class TimeSeries(
@SerializedName("sourceInfo")
val sourceInfo: SourceInfo? = null,
@SerializedName("variable")
val variable: Variable? = null,
@SerializedName("values")
val values: List<Value_>? = null,
@SerializedName("name")
val name: String? = null
) : Serializable

Then pass it in your bundle:

val intent = Intent(context, DetailsActivity::class.java).apply {
putExtra(MY_DATA, Gson().toJson(mydata[position]))
}
context.startActivity(intent)

Then bring it in through your bundle:

mydata = Gson().fromJson(intent?.extras?.getString(MY_DATA), TimeSeries::class.java)

Passing Parcelable Object between Intents

Use data third parameter of onActivityResult method instead of getIntent() for getting data from Intent which is sent from Activity which is started using startActivityForResult :

Diner newDiner = data.getParcelableExtra("newDiner");

android - unable to pass parcelable in Intent to another activity

I recommend that you use this library: https://github.com/johncarl81/parceler



Related Topics



Leave a reply



Submit