I need to start an Activity with parameters. I know i can do it with Intents and Bundles, but as far as i know it's only possible with String, boolean, etc...
I have to do it with custom parameters, like user-made classes.
For example, i got PLC-class in main activity, which contains many variables and methods, and i need to use it in the other activity.
I know the "public static" method, but i'm not a fan of it, and i'm sure i can find something else that fits my need.
I hope you guys can help me
Bye
Or you can make your class implementing parcelable interface, and put it in a parcel that you send from your source activity to destination activity..
In my opinion you can make bean class... which have getter/setter method.. you can set value there, and retrieve those values in any other Activities.Using it class.
E.g
public class Constants{
public static Bean userBeen=new Bean();
}
Main activity
Constants.userBeen.setValue("anything");
In other Activity,, you can get value using userBeenobj;
Other Activity
String s=Constants.userBeen.getValue();
Hope this helps to you. I mostly use this.!
make your custom made class serializable and then put it in bundle like this
bundle.putSerializable(key, value);
Related
Is there any way for a Java class to know if it was called from a Fragment, or from an Activity? I'm programming for Android, if it makes any difference
I agree with #basant_matharu's comment in your post.
Even if we can't determine that, it doesn't mean that we can't really know if it was an activity or fragment.
I may be wrong here and it may be totally out of question. But, what if, it's just you who want's to know if it was an activity or fragment. In that case, can't we just pass two results; one from activity and another from fragment to suppose in some another activity and just determine if the result came from an activity or from a fragment.
For instance;
We pass some pass or just use some preference to store a value from ActivityA : maybe a string result.
We do the same from fragment and just use a preference in fragment too. : maybe a string result again.
Now, in the third activity, we get two values; one from activity and one from fragment.
After that we just compare the results and see where the value came from. Like,
If the value came from activity, String resultfromactivity; return activity(SomeStringPlaceholder) and do some code here.... and same compare with Fragment.
In that way we just know if the value came from Activity or Fragment.
Again, I may be wrong here..So, if someone wants to edit or make suggestion to this, I will be happy to accept that.
If you can change the calling code, you can always use something like the following:
public class MyClass {
public MyClass(boolean fromActivity) {
if (fromActivity) {
// handle call from activity
}
else {
// handle call from fragment
}
}
}
If you have anything other than Acitivities/Fragments calling your code, you could always use an enum.
This is possible by checking the call stack - check this question to read how to get know how to do it:
Get current stack trace in Java
You can put it into your class constructor and just check what called it.
However:
This is usually not a good way to write programs. The better way to achieve it is just to add some parameter in constructor, create factory method for class or use one of may other possibilities that are not against common used programming patterns.
I need to pass a list of cookies from one activity to the other using an Intent. How would one go about achieving this?
Thanks.
Using Intent.putExtra(String name, String[] value) would be the easiest way to pass a list of cookies. There are other putExtra signatures as well, depending on how you currently have your list implemented. On the other side, you would use getExtras() to get values. If for some reason, you had a more complex setup, you could create a Cookie class which extends Parcelable.
Just pass it along in the setExtra() method of the Intent class.
Hope it helped,
JQCorreia
hi to all
I what to know how to save data in next activity
Use Intent to pass data from one Activity to another Activity.
But these Intents may carry native data types like String, int, float, double etc.
If you want your own Object to be passed, you would have to implement Serializable interface.
An odd way is to use a static Object in a class which is globally accessible.
you should explain clearly about your requirement. As i have understood, i think you want to access data which belongs to one activity in the other activity.
Refer the link below, you will get a clear idea.
http://developer.android.com/resources/faq/framework.html#3
u can use intents, bundles, or any other static variables to holds data from one activity to another
HI all,
actually i m new in android development.
i have one activity class and one simple class(Without Activity). in Simple class i m calling some web service and storing the response in array. after that this array i want to access in my activity class. m not able to make the object. how can i do that? i don't want to use bundle. as this array i want to access in more than one class.
please help me.
thanks in advance.
The activity calls/creates the Simple class, right? Pass a reference to the activity object to the Simple class, have the Simple class call the activity back with the data.
You can also use static, but it depends of data.
HI,
i'm programming an android application using the StartActivityForResult() - Method.
But, i want to get as result an own class, not a string or something like that.
how can i tell the intent to use the class i wrote to give back?
You have to have your class implement Parcelable or Serializable. Also, this technique is only recommended for activities within the same application.