Passing Arraylist data from one activity to other in OnPostExecute - android

I am trying to pass arraylist from one activity to other using Bundles in OnPostExecute method,i am not able to do so.I am not getting proper error also to typecast or do stuffs to remove error.I am not sure what is wrong here.
***here reminderList is List<GetReminder> reminderList;***
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
#Override
protected Void doInBackground(String... params) {
//Invoke webservice
vaildUserId=WebService.invokeAuthenticateUserWS(loginUserName, loginPassword, "AuthenticateUser");
if(vaildUserId>=0){
reminderList=WebService.invokeHelloWorldWS("GetReminder");
}
return null;
}
#Override
protected void onPostExecute(Void result) {
Bundle bundle = new Bundle();
bundle.putStringArrayList("reminderList", reminderList);
reminderIntent.putExtras(bundle);
startActivity(new Intent(getApplicationContext(), ReminderActivity.class));
startActivity(reminderIntent);
}

To solve your problem, you can use putParcelableArrayListExtra() and getParcelableArrayListExtra() methods which are defined in the Intent class.
1.Make sure your GetReminder class implements Parcelable .
Here is the document of Parcelable and it also contains a typical implementation of Parcelable.
Here is a website which can help you generate an Parcelable implementation of a class automotically.
2.In your onPostExecute() method put extra like this:
//Remember to declare reminderList as ArrayList here.
ArrayList<GetReminder> reminderList = ...;
Intent intent = new Intent(getApplicationContext(), ReminderActivity.class);
intent.putParcelableArrayListExtra("reminderList", reminderList);
startActivity(intent);
Then in your ReminderActivity class get the ArrayList like this:
ArrayList<GetReminder> list = getIntent().getParcelableArrayListExtra("reminderList");
BTW, there is also another way to solve your problem, you can refer my answer here.

The problem is your List<GetReminder> reminderList; is not a String List.
To pass Custom Object List you have to make your Custom class either Serializable or Parcelable. In your case make GetReminder as Serializable or Parcelable.
Then use putExtra() or putSerializable() of Intent to pass Serializable object.
Also some supercilious code I noted from your onPostExecute() as you have written
startActivity(new Intent(getApplicationContext(), ReminderActivity.class));
startActivity(reminderIntent);
will cause creating two activity instance.
so remove first one,
startActivity(new Intent(getApplicationContext(), ReminderActivity.class));

Related

Send Data between Activities as done with fragment

I have a data of custom List type (finalHolder.data)
Earlier I had to send this data to a Fragment constructor it is working fine
Fragment fragment;
fragment = new RouteMap(finalHolder.data);
((Activity)getContext()).getFragmentManager().beginTransaction().replace(R.id.content_frame,fragment).commit();
The Fragment constructor code
public RouteMap() {
// Required empty public constructor
}
public RouteMap(MyListEntity data) {
this.data = data;
}
Activity constructor :
public Map() {
}
public Map(MyListEntity data) {
this.data = data;
}
But now I want this same data to reach a Activity (Map2)
I tried putExtra with intent but Intent but it doesnt work
In Short I want the "finalHolder.data" to be send to Activity constructor while calling it .
If it isnt possible then please suggest solution
Thanks
It is not recommended to use Activity constructor. To send data object to activity you can use Intent extras with Parcelable. For that you need to do the follow way.
Implement your data class from Parcelable
Ex: MyListEntity implements Parcelable
Override the following methods in your data class(MyListEntity) writeToParcel(), readFromParcel()
3.Implement the CREATOR class in your data class(MyListEntity)
Reference link for Parcelable example: passing object to activity
Send the data object using Intent as mentioned below
Intent i = new Intent(this,Activity(Map2).class);
i.putExtra("mylistdata",finalHolder.data);
startActivity(i);
And finally get the same data at your Activity(Map2) onCreate() method as mentioned below
MyListEntity listEntry = getIntent().getParcelableExtra("mylistdata");
Now your object is ready at your second activity.
Hope this will help for you.
To send an object in intent the object's class must be serializable so make your
MyListEntity implements serializable then use the Intent's putExtra(String name, Serializable value) and in the other activity use the intent's getSerializableExtra (String name) but don't forget to cast the returned value
One option could be letting your custom class implement the Serializable interface and then you can pass object instances in the intent extra using the putExtra(Serializable..) variant of the Intent#putExtra() method.
Your data should implement Serializable ie
MyListEntity implements Serializable
//Use this to send the data
intent.putExtra("MyClass", finalHolder.data);
// To retrieve object in second Activity
getIntent().getSerializableExtra("MyClass");
Regarding your Fragment: it is not smart to pass data via c-tor to fragments!
Use arguments mechanism:
Fragment f = new SomeFragment();
Bundle args = new Bundle();
args.putExtra("data", someData);
f.setArguments(args);
And retreive them:
getArguments().getParcelableExtra("data")
More info here.
But (to return to your question), for that, your data have to implement Serialisable (Java approach) or, by Google advised Parcelable (Android style)
More info about Parcelable here.
Implementing Parcelable interface can be boring and error prone, so some cool people created this: http://www.parcelabler.com/

Pass an interface from class to activity

If i have an object form interface in my class, how i can pass it to activity?
My solution was to change it as static object, it's work fine but sometimes it create a memory leaks because of all the old references which the garbage collector cannot collect, and i cannot implement 'Serializable' on my interface.
public class MyClass {
protected OnStringSetListener onstringPicked;
public interface OnStringSetListener {
void OnStringSet(String path);
}
//......//
public void startActivity(){
Intent intent=new Intent(context,Secound.class);
// ?! intent.putExtra("TAG", inter);
context.startActivity(intent);
}
}
In my opinion, using Bundles would be a nice choice. Actually bundles internally use Parcelables. So, this approach would be a good way to go with.
Suppose you would like to pass an object of type class MyClass to another Activity, all you need is adding two methods for compressing/uncompressing to/from a bundle.
To Bundle:
public Bundle toBundle(){
Bundle bundle = new Bundle();
// Add the class properties to the bundle
return bundle;
}
From Bundle:
public static MyClass fromBundle(Bundle bundle){
MyClass obj = new MyClass();
// populate properties here
return obj;
}
Note: Then you can simply pass bundles to another activities using putExtra. Note that handling bundles is much simpler than Parcelables.
Passing in custom objects is a little more complicated. You could just mark the class as Serializable
and let Java take care of this. However, on the android, there is a serious performance hit that comes with using Serializable. The solution is to use Parcelable.
Extra info

Pass 3D-array to Activity in Android

I am trying to pass a 3D-doublearray to another activity. I don know why I get NullpointerException? What is missing?
MainActivity
Intent intent = new Intent(this, DataActivity.class);
Bundle mBundle = new Bundle();
mBundle.putSerializable("list", output_data);
intent.putExtras(mBundle);
startActivity(intent);
DataActivity (reveiver)
Intent intent = new Intent();
double[][][] params = (double[][][]) intent.getExtras().getSerializable("list");
And I know for certain that the 3d-array already is allocated in the MainActivity. I have tested that!
Would be glad if someone has any solution to this and could answer why I get a NullPointerException.
(Edit: NullpointerException at the line double[][][] params = ...)
I'm pretty sure double[][][] doesn't implement Serializable. What you can do is make your own class like so:
public class MyArrayWrapper implements Serializable {
private double[][][] arr;
public MyArrayWrapper(double[][][] value) {
arr = value;
}
public double[][][] getArray() {
return arr;
}
}
You then instantiate that wrapper class and put it as a serializable instead. Then when getting the serializable you do it like this:
MyArrayWrapper wrap = (MyArrayWrapper) intent.getExtras().getSerializable("list");
double[][]][] myArray = wrap.getArray();
Hope this helps!
Maybe you need a class that implements serializable?
Passing data through intent using Serializable
In this example, they are using custom class and List<> to serialize in a bundle. You can create a class with the 3D array inside?

Android,access a reference from another activity

Good day,
I have my main activity with an object,
public Network netConnection = null;
in my main activity i then call:
netConnection = new Network(new Network.OnMessageReceived() {
#Override
// here the messageReceived method is implemented
public void messageReceived(String message) {
// this method calls the onProgressUpdate
publishProgress(message);
}
});
netConnection.run();
Now i create a new activity and i run it with this code:
case R.id.menu_packet: {
Intent intent = new Intent(this, PacketActivity.class);
String id = "" + hashCode();
intent.putExtra("id", id);
startActivity(intent);
return true;
}
I have tried doing things with putExtra() in the intent etc. But i have not come right.
Is there not an easy way to just pass a reference to PacketActivity of my netConnection ?
I don't want to copy it or any thing. just be able to access the netConnection object from the PacketActivity?
Thanks
You can extend Application, create setter and getter method in your extended application, and then call it from new activity.
here a tutorial
useful links:
Extending Application
Extending Application to share variables globally
Android extends Application basics
e.g.
public class myApplication extends Application{
private myType myObj;
public void set_myObj(myType theThing){
myObj = theThing;
}
public myType get_myObj(){
return myObj;
}
}
then from you main activity:
((myApplication)getApplication()).set_myObj(myObj);
and from second activity:
myType myObj = ((myApplication)getApplication()).get_myObj();
and be careful with memory leaks..!
This sounds like a use case for a service:
http://developer.android.com/reference/android/app/Service.html
The thing is once the activity from which you created the Network object is destroyed e.g. to launch the new Activity then the Network object will also be garbage collected so I don't think passing a reference would help...

Use Parcelable to pass an object from one android activity to another

I want to do this
class A extends Activity{
private class myClass{
}
myClass obj = new myClass();
intent i = new Intent();
Bundle b = new Bundle();
b.putParcelable(Constants.Settings, obj); //I get the error The method putParcelable(String, Parcelable) in the type Bundle is not applicable for the arguments (int, A.myClass)
i.setClass(getApplicationContext(),B.class);
startActivity(i);
}
How do I use Parcelable to pass obj to activity B?
Create your class and implements Serializable:
private class myClass implements Serializable {
}
And do like:
myClass obj = new myClass();
Intent aActivity = (A.this, B.class);
intent.putExtra("object", obj);
On Receiving side:
myClass myClassObject = getIntent().getSerializableExtra("object");
As the error suggests, you need to make your class (myClass in this case) implement Parcelable. If you look at the documentation for Bundle, all the putParcelable methods take either a Parcelable or a collection of them in some form. (This makes sense, given the name.) So if you want to use that method, you need to have a Parcelable instance to put in the bundle...
Of course you don't have to use putParcelable - you could implement Serializable instead and call putSerializable.
Parcelable is pain in writing code but more cost effective than Serializable. Have a look at the given below link -
Parcelable Vs Serializable

Categories

Resources