I need to pass object user to another activity. I know I should use Parcelable, but I can't edit User class, because it is used from maven repository.
Is it here any other way how can I pass user object? Or how to locally edit class from maven repository?
You can use the Application class for this purposes.
what you need is:
create a class which derives from Application.
declare the class in the AndroidManifest.xml (see here)
create a object variable of type User in it (also getter and setter).
assign the user object in first activit through:
MyApplication app = (MyApplication) getApplication();
app.setUser(user);
retrieve the object in other activity through:
MyApplication app = (MyApplication) getApplication();
User user = app.getUser();
BUT: take care, after you restart you app (go in background and open it again), a new Application object will be created, where the User is null, take care of that then.
This problem can be read here.
Hope this helps.
You can serialize the object into some kind of string representation. The easiest way to do this is to convert the object into a JSON string, pass the string to the other activity via intent and convert the string back into the original object.
What I am doing is just converting the object into Json and then sending as a String using Intent. Then you can't catch it and parse Json.
P.S. Using google's Gson class making this very easy. https://code.google.com/p/google-gson/
Related
I am trying to get a specific Member object from a Guild by using the following:
event.getGuild().getMember(user)
Here, user is the User object of the user I want to get the Member object from. I have also tried using the .getMemberById, using the userID instead. However, in both cases I get a null-pointer exception.
I am sure that both my User object and userID are correct, because when adding a breakpoint it does show these, but it doesn't retrieve the Member, this stays null. Am I approaching this wrong?
I have also tried putting the following in my main file where I start the bot:
JDABuilder builder = JDABuilder.createDefault(BOT_TOKEN);
builder.enableIntents(GatewayIntent.GUILD_MEMBERS);
On the Discord developer portal I have enabled both the 'Privileged Gateway Intents' options.
The documentation of Guild#getMember tells you to see also Guild#retrieveMember which can be used to load members that are not cached.
If you have an event with a getMember() or retrieveMember() method, that should be used instead. Otherwise you can do this:
guild.retrieveMember(user).queue(member -> {
... use member here
});
Other alternatives are outlined in the JDA wiki here: Gateway Intents and Member Cache Policy
Most of these retrieval methods DO NOT require the privileged intent GUILD_MEMBERS. It is only required if you want to interact with the entire member list of a server, through methods like loadMembers or findMembers for example.
I'm using 'twitter4j' to make a twitter client app. and I want to bypass the Twitter object from the MainActivity.java to different fragments in my app to be used by them. what is the best way to do that?
I don't know if i can do it with 'Bundle' ?!
and when I tried to make a constructor for my fragment for example:
public Fragment_a(Twitter twitter)
{
//
}
i got a warning from eclipse saying that:
"Avoid non-default constructors in fragments: use a default constructor plus Fragment#setArguments(Bundle)"
thanks
You could implement the twitter object in your activity, and then just let your fragments grab it
twitterObject = ((MyActivity)getActivity()).getTwitterObject();
if (twitterObject != null)
{
// do something
}
What is different between static field and other ways in store data when the application is run?
I'm using static fields for pass data between activities and worked perfectly:
I defined this class :
public class Info
{
public static int ID = 0;
public static String NAME = "TEST";
public static TestClass testclass = null;
}
and I can store my data anywhere:
Info.ID = 5;
Info.NAME = "USER!";
Info.testclass = new TestClass();
and I can get my data anywhere:
Info.ID
Info.NAME
Info.testclass
It is usual to pass data between activities using extras within the intent. Such data persists for the lifetime of the receiving activity (when finished with, the garbage collector can free up the memory).
Or you can store values using SharedPreferences. These will persist between sessions and are stored as key/value pairs in a file (so don't impact memory use as such).
Or you can hold values in static fields (as you are doing here) which persist for the lifetime of your application session. However there is a significant risk with this in that the garbage collector cannot free up memory that is referenced by such fields unless you set them to null when you no longer need the reference. You should never store a reference to an activity/context/view in a static field since you'll leak the entire activity which can amout to a significant amount of memory usage.
http://android-developers.blogspot.fr/2009/01/avoiding-memory-leaks.html
You can pass a class instance within an intent if it is Serializable, e.g.:
Intent intent = new Intent(this, whatever.class);
Bundle b = new Bundle();
b.putSerializable("data", my_object);
intent.putExtras(b);
startActivity(intent);
And in the receiving activity, cast it back to whatever class your object is:
Bundle b = getIntent().getExtras();
my_object = (whatever class is it) b.getSerializable("data");
Many java classes implement Serializable and it is very simple to make your own classes serializable too.
If you're changing activities I'm assuming you're using intents. What you can do is send data with the intent with myIntent.putExtra("some string",someString);.
Then you can receive the info in your new activity using
Intent intent = getIntent();
String someString = intent.getExtra("some string");
You can use intents for passing data between activities.
Your first Activity.java
public void onClick(View v)
{
Intent timer = new Intent (FirstActivity.this,SecondActivity.class);
timer.putExtra("beefType", 5000);
startActivity(timer);
}
Then in your SecondActivity.java file do:
nt beefType = getIntent().getIntExtra("beefType", -1);
You want to share data between activities,you can use intent or shared prefernce.
The difference in using these tow and static data is that,intent and shared prefrence ,at some static data can be empty or null.but sending data using above two methods gurantees that you will get data in next activity ,unless you forcefully remove preference
you can refer this link for more info Static class in Java (Android) - use or not use
There is something called an Application Class in android. Its like a global singleton.
In other words, that Application Class will be common for that entire application.
Application class will be that first class to load.
So it will be easier to store some randomly used values in the application class.
public class Info extends Application
{
public static int ID = 0;
public static String NAME = "TEST";
}
Then call it in any activity by:
Info info= ((YourApplication)this.getApplication());
And in your manifest:
<application
android:name="<your package name>.GlobalApplication">
........
........
</application>
Well, that way doesn't always work in Android. Your static values are hold only while your app is running. Imagine you are sharing some content with action SEND, so you are starting another app which actually share your content (Facebook, email, etc.). Android may decide to stop completely your app if there are no resources enough to launch other app. In that point, the process of your app is completely gone and, with it, your static values. When going back to your app from the app that shared the content, you've lost your values.
I'd better use Intent extras, combined with Parcelable objects if you need to serialize more complex data.
You can easily try it if you enable in a device the option Don't keep activities under developer options, which destroys every activity as soon as the user leaves it.
I am developing a Jenkins plugin, I have an object that I want to bind it under root url.
But I have no idea how to bind.
Suppose my object is MyData which have getData method, I want to bind it to [http://localhost/MyData/data], so that I can get JSON data from this url for my ajax call.
I know the binding mechanism of hudson is Stapler, I try to annotate ExportedBean on my class, and implements it a ModelObject, but it is still failed.
Any one know how to do this?
I have read below document, hope it can help.
https://wiki.jenkins-ci.org/display/JENKINS/Exposing+data+to+the+remote+API
http://stapler.java.net/apidocs/
You want to create a RootAction.
It could implement Action interface on the Object,
then try to add it to the actions of root Hudson,
like this,
static {
Hudson.getInstance().getActions().add(new MyData());
}
the binding url is depended on how you implement getUrlName() method.
Is there's a way to get the value returned by a java controller method in javascript in the views ?
what I want to do is:
i'm in view showX rendered by controller method X.show()
i want to create an object y so $.post('#{Y.create()}')
now i need the id of the created object of type y to use it in the same view (showX).
is that possible?
It sound like what you need (although your question is very vague), is to return JSON from your controller method.
Such as, in your controller, you can do
public static void myActionOne() {
renderJSON(myObject);
}
And then you will call myActionOne from your javascript using $.post. I would also suggest looking at the Play jsAction tag if you are not already using it. This will return a JSON representation of the object. You can then take whatever information you need and call a second action in the same way.
Again, in the second action, I would suggest jsAction, as it makes passing parameters into your actions far easier.
EDIT:
Based on your edit, then all you need to do is in your controller method Y.create, do something like
public static void create() {
MyObject obj = new MyObject();
obj.save();
Long id = obj.id;
renderJSON(id);
}
Obviously your code to create your object will be different, but you get the idea. You can then just take the data from the JQuery post response, and access the id that has been returned, using standard javascript.
You question is too vague. But you probably will need of AJAX to get a value of this kind.
Take a look here: http://www.oracle.com/technetwork/articles/javaee/ajax-135201.html