First of, sorry for the bad title, I couldn't think at all what to call this.
Say if I have a string with a value of "ActivityMain", and I have a Activity in my project called ActivityMain. Is there anyway I can get a new instance of the class by the string?
The overall idea is to request data from a server, what sends back some different Activity classes, then I want to start whatever activity is returned.
Assuming you can get the fully qualified name for the activity's class, you could follow this answer and use:
Class<?> act = Class.forName("com.bla.TestActivity");
I think if else if ladder may do what you want.
if(responsefromsever.equals(NameOfActivityInString)){
// instantiate the activity
}
use case:
I am assuming there exist a Activity class whose name is MainActivity. And what you receive from server is response, (i.e String)
String nameOfActivity = "MainActivity";
if(response.equals(nameOfActivity)){
MainActivity instantiation or whatever you want to do
}else if(response.equals("SomeOtherActivity")){
//SomeOtherActivity or whatever you want to do
}
The overall idea is to request data from a server, what sends back some different Activity classes, then I want to start whatever activity is returned.
A simple way hat is coming to me is that you can have some switch condition or if-then-else conditions which would compare the string received and accordingly start the desired activity. Eg:
if ( stringReceived.equals("ActivityMain"){
//start ActivityMain
} else{
//others...
}
This might be useful, if there are not many activities to start.
It seems like you are sharing implementation details to your server of your client program. What if you want to hook up the server to an iOS app? Then the whole activity name returning idea wouldn't work. "Drawing a different line" for your interfaces might be the best option.
Related
I'm using Xamarin.Android to build an app for a customer that's meant to be supplementary to another app. The idea is that once they reach a point in their workflow where my app steps in, they will click a button inside their app that will launch my app, and will open an activity from my app based on some attached parameters.
My problem is that in the "calling" app, all that's exposed for me to work with is a parameterized URI that's passed directly to Android, so I have no means of creating and passing an Intent object. I can change the URI it sends to be whatever I like, so I've already added an intent filter that looks for the custom scheme myapp://.
I am new to SO, so I apologize if this question has been answered elsewhere, but I have looked for a few hours and in my searches so far, all I've seen are answers related to how to call another app from my own. The question I have is... how do I parse that request on the other end and know not only which activity to open, but the rest of the data or parameters that were in the URI? Is it possible to open an activity based on a parameter in that URI, or can I only point it to one activity?
Thanks in advance!
What you have to do is quite simple in your URL somewhere pass your activity name that you want to navigate to
Then in your code maintain a enum with all the possible activities you wanna navigate to:
enum ActivityName
{
MainAcitivity,
SomeotherAcitivity
}
Then in your received notification get the string where you have given the name of the activity and do something like this :
If(youractivityName==ActivityName.MainActivity.toString())
{
StartActivity(typeof(MainActivity));
}
Revert in case of queries.
Many times we use intents to send data to a Fragment or get data back from a child. Can't we just put data in a public variable?
For example imagine if we want to get data from user from a dialog box.
I'm just talking about the "possibility". Undoubtedly, It is superior to use intents for code cleanness or safety...
you don't send intent's to fragments, if you want to use objects you need to have your object implement Parcelable then you can just send the object in the intent bundle
public class MyActivity extends Activity {
public int someValue = 1;
}
And in any fragment which has MyActivity as a host you can access ((MyActivity) getActivity()).someValue.
I think what he means is sending (local)broadcast... which is by the way the proper way of doing it according to my understanding.
Of course it is possible to have public (or even protected) fields and access them from a child-fragment with something like this:
assuming your parent activity is named "MainActivity"
((MainActivity) getActivity()).mMyPublicField
or:
((MainActivity) getActivity()).getPublicMethod()
- but I would never recommend doing this!
especially when you also start manipulating the public field you can run into ugly trouble when different threads are in play.
If something needs so be shared across the whole application, use SharedPreferences (if you want to store it for the next app session too) or as I mentioned first LocalBroadCastManager.
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.
is it expensive to always pass Activity as an argument to a method?
You see, I have this class (this is where I put the commonly used methods by my activities). And all my methods there have Activity as a parameter, because the results of those methods will need to be send back to the activity that invoked the method, so basically I need to know to whom should I throw back the results, so I always have like:
public static void processThis(Activity activity){
// some Code
}
I just like to know if this is against the best practices or if this is expensive to use?
Thanks. Looking forward for your explanations that will enlighten me.
Added:
I'm extracting commonly used methods from my activities to promote code reuse. Like, ActivityA use methodA and send broadcast back to ActivityA, same way goes for ActivityB which uses methodB(same as methodA) and send broadcast back to ActivityB. So what I want to achieve is to to extract that methodA and methodB which basically are the same, and put them into another class, and add a parameter Activity so I can know to whom do I need to send the broadcast back. Thanks.
In Java, all parameters that are not primitives are implicit pointers; consequently, passing an object such as activity only requires passing around the address at which the Activity is located in memory, so passing such a thing is not an expensive thing to do in Java.
So, the real question you should ask yourself is not whether it is expensive, but rather if it makes the most sense (from a logical / maintainability perspective). If it makes sense, then by all means do it.
This is a pretty simple question, but I have been unable to find anyway to accomplish what I am trying to do...
I want to launch a new Activity to display some complex information. Because of the complexity, it is undesirable to serialize the information into the intent's parameters. Is it possible for the the new Activity to get a reference to the launching activity, so it can call its methods?
If you use a custom application class, you can store information that will be kept between the activities.
See a tutorial here for instance.
The lifetime of an Activity cannot be depended upon. In this case, one way of sharing data is to have a singleton which holds the data to be shared between the two activities.
You can add a public static field to the first activity containing this (the first activity).
But beware that the first activity could be destroyed by Android while you are using the second activity, so you will have to implement a fallback method if the first activity is destroyed.
And don’t forget to unset the public static variable in the onDestroy() callback of the first activity or you will leak memory.
Is it possible for the the new Activity to get a reference to the launching activity, so it can call its methods?
Please do not do that. Android can and will destroy activities to free up memory.
Complex information like you describe should not be owned by an activity. It should be held in a central data model, like you would in any other application. Whether that central data model is mediated by a Service or a singleton or a custom Application object depends a bit on the type of data, caching models, risks of memory leaks, and so on.
You can make your complex objects public and static in ActivityA, and access them in ActivityB like this:
MyCustomObjectType complexFromA = ActivityA.complexObject;
this will work, however while in ActivityB, you can't always be sure that static objects from ActivityA will exist(they may be null) since Android may terminate your application.
so then maybe add some null checking:
if(null == ActivityA.complexObject) {
//go back to ActivityA, or do something else since the object isn't there
}
else {
//business as usual, access the object
MyCustomObjectType complexFromA = ActivityA.complexObject;
}
You could also use a Singleton object which extends Application. You would have the same problem when Android terminates your application. always need to check if the object actually exists. Using the Singleton extending Application approach seems to be the more organized way - but adds more complexity to implementation. just depends what you need to do and whatever works for your implementation.
You should create a separate class that both the activities can use.
public class HelperClass{
public void sharedFunction(){
//implement function here
}
}
I would recommend staying away from static variable in android. It can cause some unexpected behavior.
Use getParent() from new activity and call parent's method
Android Activity call another Activity method