How to judge whether two context are the same in Android? - android

such as you want to judge two strings, you can use string1.equals(string2)...then how to judge whether two context are the same in Android?

Why do you do this?
If they are all Activity instances you can treat them as such and use:
if ( activity instanceof MyClassActivityOne ) {
// do something
}

Check it like this
if(c1.getClass().equals(c2.getClass()))
{
//The context is the same
}
else
{
//Context is different
}

You can check the getApplicationContext() of each to see if they are the same.

Im not sure,but you can try this:
if(context1.getClass().getName().equals("com.xxx.sameclass"))&&context2.getClass().getName().equals("com.xxx.sameclass")))
{
if(context1 == context2)
//same condition
}

Related

How to get the name of the activity from the fragment?

So basically i am going to use one Fragment in two different activities.Except one method of fragment in which I want to change something.So how do i get the name of activity which is using the Fragment so that I can do things depending upon the name of which activity is current.
in Java try:
getActivity().getClass().getSimpleName()
But be careful when you're using getActivity() method from fragment. If your fragment is not attached to activity getActivity() will return null.
in Kotlin try:
activity?.javaClass?.simpleName
It's null safe
The first Ans is great but it's in java so i translate this in koltin
activity?.javaClass?.simpleName
Firstly check if the fragment is still attached to activity, then you can check for activity name:
if(isAdded()) {
getActivity().getClass().getSimpleName();
}
Create your own interface and implement it in both of your activity and finally pass this instance to your fragment.
public interface ActivityListener
{
void onClick();
}
write your code into onClick() method and call this method from fragment.
Use this.getClass().getSimpleName() to get the name of the Activity.
if you're in the context of an OnClickListener (or other inner class), specify the class manually:
MainActivity.class.getSimpleName()
for more details check this link
I think an better solution will be to create an enum which differentiate your cases and sent that enum through the arguments of the fragment. In this way your cases will be very clear and you will know why there is a difference in the flow of your fragment.
If you're in another class and you're trying to get the name of the "initiating class" then you can use Context to access it, like so: getContext().getClass().getSimpleName();
Example:
public String getMyActivityName() {
String myActivityName;
myActivityName = getContext().getClass().getSimpleName();
return myActivityName;
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
Toast.makeText(this.getContext(), "myActiveParentClass: "+getMyActivityName(), Toast.LENGTH_SHORT).show();
}
I hope it helps someone...

How do I pass List <PackageInfo> between activities in Android?

I am trying to pass a List of PackageInfo between multiple activities in my application. Is it possible to do this using an Intent?
You can again get package info in second activity, if you do want some operation to be done based on package, pass array of package names (com.google.) from one activity to other.
You can put anything in the intent's extra, the object just have to be parcelable (seems complicate but it's not at all!).
You have example here : http://developer.android.com/reference/android/os/Parcelable.html
Maybe a concrete list is parcelable (like ArrayList) I don't remember.
The other way I'm thinking about is maybe to use the shared preference, I know you can put a set in there, if you wan't to access your list everywhere that can be a way to achieve it.
Hope I help !
In that exact case you can query the PackageInfo list in every activity. But in the common case, when I have to share non-parcelable objects through multiple activities, I use to create a singleton class for that. Note that it would consume memory, so make sure to dispose the singleton when it's not needed anymore.
For example, for PackageInfos it would look something like this:
public class SharedPackageInfos{
private SharedPackageInfos(){}
private static SharedPackageInfos _current;
public static SharedPackageInfos getCurrent()
{
if(_current == null)
{
_current = new SharedPackageInfos();
}
return _current;
}
private List<PackageInfo> packageInfos;
public List<PackageInfo> getPackageInfos()
{
return _packageInfos;
}
public void setPackageInfos(List<PackageInfo> packageInfos)
{
this.packageInfos = packageInfos;
}
public static void dispose()
{
_current = null;
}
}
And from the activities call
SharedPackageInfos.getCurrent().setPackageInfos(myList); //save a list
List<PackageInfo> packageInfos = SharedPackageInfos.getCurrent().getPackageInfos(); //retreive a list

How can I get a view from a Context in Android?

in my project I am passing a context of an activity to a helper class. Now, is it possible to use that context and find the views from that activity? Basically, I would like to find the views by id, but just using a context object.
How can I achieve this?
Provided you stored your context in a private reference, do this (MonoDroid)
View parent = ((Activity)_context).Window.DecorView.FindViewById(Android.Resource.Id.Content);
In Raw Android it'd look something like:
View parent = ((Activity)mContext).getWindow().getDecorView().findViewById(android.R.id.content)
Activity is a Context, so if you actually pass your Activity to a helper class, you can just:
void someMethodInHelperClass(Context c) {
if(c instanceof Activity) {
((Activity)c).findViewById(R.id.someviewid);
}
}
Of course it will be much easier if you change your method to:
void someMethodInHelperClass(Activity c) {

How do I synchronize this properly?

public void consumeResponse(OmwListResponse<T> response) {
synchronized (response.getResultList()) { // XXX this isn't synchronized safely
for (T t : response.getResultList()) {
if (!cacheList.contains(t)) {
cacheList.add(t);
}
}
}
}
The the situation is I don't want anyone to chance response.getResultList() or cacheList until this method is done. How do I properly do this?
Create a lock object:
private static final void LOCK = new Object();
and synchronize on that.
Synchronizing cacheList is easy. Just wrap any code your code that uses it in:
synchronized(cacheList) {
// Make changes to cacheList here
}
If cacheList is a public member and you're afraid external classes will change it, make it a private member and synchronize the getter and setter. This is the only way since you have no control over what other classes do and it is your responsibility to synchronize your members.
As for response, that is trickier because I don't what an OmwListResponse is. Do you own that class? If so, use the same method as above. If not, you may be out of luck.

Android -to call another class

can anyone tell me how to call another class which does not extend activity , I have searched in many forums I am not clear with that ...
Thanks in Advance
Based on your question and your response to other answer, i guess you want to do something like this,
class UploadImages
{
UploadImages()
{
}
void uploadImagesFunc()
{
//some operation;
}
}
//In Activity, do something like following
UploadImages mUploadImages = new UploadImages();
mUploadImages.uploadImagesFunc();
see this
class_name variable = new class_name();
variable.method_name();

Categories

Resources