How to send an object from activity to fragment? [duplicate] - java

This question already has answers here:
Passing an Object from an Activity to a Fragment
(8 answers)
Closed 7 years ago.
I just want to use getActivity().data in my fragment to pull in the data object.
It's not working though. Eclipse says that data cannot be resolved.
It looks like there is plenty of information about how to use Bundle to do this.
However, I am wondering how to use getActivity in particular.

Although using Bundle is the preferred way and following way must be avoided but just to answer your question, try making the data variable static and access it by typecasting the activity ((YourActivity)getActivity()).data.

Related

Can't use getFragmentManager() [duplicate]

This question already has answers here:
Since the Android getFragmentManager() API is deprecated, is there any alternative?
(14 answers)
Closed 1 year ago.
I have a fragment and I want to add its data to another fragment via getFragmentManager() but this metod has been deprecated and now I don't know what to do.
Screenshot
You can use FragmentActivity.getSupportFragmentManager().
Visit Since the Android getFragmentManager() API is deprecated, is there any alternative?
for more details .

Issues with getDrawable compatibilty [duplicate]

This question already has answers here:
Android getResources().getDrawable() deprecated API 22
(16 answers)
Closed 4 years ago.
This is the line in question. I have it the same way the textbook lists it, but am having issues converting the line to ContextCombat since getDrawable() has been deprecated.
buttonItem.setImageDrawable(getResources().getDrawable(painting.getId()));
You can try using ContextCompat.getDrawable(getActivity(), R.drawable.name);. This will return a styled Drawable as instructed by your Activity's theme. Your code would be more or less as shown below:
buttonItem.setImageDrawable(ContextCompat.getDrawable(getActivity(), painting.getId()));
NOTE: I'm assuming that painting.getId() returns the resource id for the image you want to set as your button's drawable. Also, if you are doing this inside an Activity, you should use the keyword this instead of getActvity().

Is there any lifecycle method which will get called each time when a fragment is opened? [duplicate]

This question already has answers here:
ViewPager with fragments - onPause(), onResume()?
(10 answers)
Closed 4 years ago.
I tried to use onStart() but it doesn't invoke with ViewPager in some cases, especially when going to fragment from nearest tab on tablayout.
What can be problem?
Is there any other events that invoke explicitly?
Fragment#onResume() will be called when the fragment is visible to the user and actively running. This is generally tied to Activity.onResume of the containing Activity's lifecycle.

Is activity same as Java Class in android? [duplicate]

This question already has answers here:
Classes vs Activities in Android
(2 answers)
Closed 4 years ago.
I have just started learning android and am not able to understand what an activity is. My question is, is creating an activity same as creating a new class? A class is just referred as an activity in android.
Also can't a UI screen have multiple java classes or one UI page will have only one java class?
From https://developer.android.com/reference/android/app/Activity
An activity is a single, focused thing that the user can do. Almost all activities interact with the user, so the Activity class takes care of creating a window for you in which you can place your UI with setContentView(View)
We can say Activity is a Java class that the user can interact with.

How to make a rolling news banner on android? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
marquee text in android
in my app i want to have a news banner with the words moving to one side like tv .. i don't really know how to run that kind of animation .. and should the banner be a TextView ? i'm not really sure of anything .. so i just need the xml format of such thing, Thankss!
This is commonly referred to as a ticker or marquee. Here's a tutorial on how to make one.
Edit: Here's another tutorial.
Edit: Here's another stackoverflow question about the same thing.
Edit: And another.

Categories

Resources