I have an array of Classes, like this:
private static final Class[] testerClasses={
Tester1.class
};
I need it to be like this, so I can start Android intents, like this:
intent =new Intent(this,testerClasses[0]);
startActivity(intent);
I'm trying to figure out how to do the following, and I'm really struggling to figure out how to do it...
I need to call functions from an instance of the class. For the most part, these are static or static like functions, so merely getting an instance of the function is sufficient.
I need to make sure I can pass the class as the Intent structure requires.
Why do I want to do this? Sometimes I want to call the function like an intent, other times I just want to get data about the intent, without having to call it. My abstractTester class (Of which all concrete tester classes inherit from) contains a few things like the name of the test, which is handy to have before the Intent has even started.
Thanks for the help!
You will not be able to pass the class object with the Intent . Instead, pass the class name in the intent; the receiver can get the class via loadClass()
Once the receiver has the class object, it can use reflection to call the intended method.
The correct solution is:
private static final abstractTester[] testerClasses={
new Tester1()
};
intent =new Intent(this,testerClasses[0].getClass());
startActivity(intent);
Then if I need to reference the class, I can just do it. Works like a charm!
Hi you can try to use code below ,
ArrayList<Class<?>> a = new ArrayList<Class<?>>();
a.add(MainActivity2.class);
a.add(MainActivity.class);
Intent intent =new Intent(this,a.get(0));
startActivity(intent);
Related
I am knew to Android.
How do i pass a static variable from one activity to a class.
I have a list of buttons and when i click one button, i want to open a new activity and at thye same time, pass a variable relating to that class. I have tried using shared preferences but in vain. The string variable i would like to is is called url. This is the code i have from the class
public class AppConstant {
public static final String BASE_URL = url;
}
The BASE_URL will be used in other classes that is why it is static and i want it to stay that way.
shortest way would be to remove final keyword
public static String BASE_URL = url;
this will allow you to write to this field from any method in your app
AppConstant.BASE_URL = "abc";
note that AppConstant isn't proper name for such usage, BASE_URL isn't constant...
btw. keeping data in static field is a veeery poor aproach, don't do this... use Bundle for passing data between Activities, IPC communication (binding) for Services, SharedPreferences, SQLiteDatabase and probably few other methods...
Since other solutions haven't worked for you, here is something a bit different:
Make a saparate file named "ext" or something. inside make a public abstract class like so:
public interface ext {
HashMap <String, String> myHash = new HashMap<>();
}
This way myHash will be created when the app is launched and will live as long as the app does so when you move from activity to activity, you can add values this way and access them in the next activity:
ext.myHash.put("BASE_URL", url);
If it's static it shouldn't be in a Activity,
I assume you mean accessing a static variable from an Activity?
If so you can simply call AppConstant.BASE_URL in your Activity, if it is shown in red press Control+Enter to fix the import in Android Studio.
On the other hand, passing a variable from an activity to a class can simply be done via methods. So just call a method in the activity and use a String as a parameter like so
Otherclass.doSomethingWithString(urlInYourActivity)
But I assume the thing I mentioned at the start is what you want to do
I'm currently writing an App, it gets JSON from a website, and then lists the contents in a listview. Now since the API itself only returns names like this: "twisted_castle" instead of "Twisted Castle", I created a new class to substitute the generic names to the right names, which I previously added to the strings.xml.
Now I can get Strings via String test = getString(R.string.key) in the MainActivity, but since I created a new class for the Substitute to happen, I somehow can't use getString somehow.
I already discovered that I'll need to get/use the context of MainActivity somehow, but really any solution I found didn't work, and also I'm a bit irritated on how the whole context thing works.
Anyone can help me with this and maybe has a good explanation on how contexts work?
You can use Context as:
mcontext.getString(R.string.something);
all you have to do is init context in the class some where like:
static Context mcontext;
public void setmContext(Context context){
this.mcontext=context;
}
and call setmContext(this) from your activity or where ever you have context attribute
For this project, I'm using Android Studio.
I've tried a bunch of things.
Goal:
I would like to create an application which asks the user to input data. The data needs to be saved and later be called on another screen as a log history which the user can see.
What I've done:
I have three classes: "InputScreen" "Logs" "LogHistoryScreen"
I've tried to create Logs object at InputScreen which works perfectly fine such as: Logs log1 = new Logs(); But I have no idea how to call the object log1 made in InputScreen from the LogHistoryScreen. Anyone who have any suggestions?
Thanks in advance.
Create a list of logs in InputScreen class:
List<Log> logs = new ArrayList();
logs.add(log1);
// same for next logs.
then create a method in InputScreen which will return the list of logs. Something like this:
public List<Log> getAllLogs() {
return logs;
}
Call this method from LogHistoryScreen like:
InputScreen inputScreen = new InputScreen();
List<Log> logs = inputScreen.getAllLogs();
There are various options you could use here. I will mention a few here.
1) Passing through Intent.
Pass the Object as a parameter while you open the LogHistoryScreen from the InputScreen.
Please follow the below code to do so.
Intent intent = new Intent(this, LogHistoryScreen.class);
intent.putExtra("Key", yourObject);
startActivity(intent);
Receive the Object in the onCreate() of the LogHistoryScreen activity
Log log = (Log)getIntent().getSerializableExtra("Key");
The Log.class must implement Serializable.
public class Log implements Serializable
{
}
2) Store the Object in the Database and retrieve it from the other activity. This is particularly helpful if you need the data to persist across app sessions.
I think the first option will be more helpful for you.
I wish to pass an arbitary object via an Android intent, I have the following code:
In the activity I wish to send an intent from:
Intent intent = new Intent(containerListActivity, com.uk.jacob.containerdroid.activities.ContainerDetailsActivity.class);
intent.putExtra("container", containers.get(i));
containerListActivity.startActivity(intent);
The activity I wish to recieve an intent:
System.out.println(getIntent().getExtras("container"));
I seem to get the error "cannot resolve method putExtra.
containers is essentially a Jackson JSON mapped POJO.
Unfortunately, you can't put an arbitrary object in the Intent. It needs to be Parcelable or Serializable.
http://developer.android.com/reference/android/content/Intent.html
You can do it a few ways:
Have your custom class implement Serializable. This is quick to code, but may not be performant.
Have your custom class implement Parcelable. This could be a bit of work.
Convert your container to a string using Jackson, then convert it back after using getStringExtra.
Serialize your JSON to a string and then push it to the intent. In the receiving activity try getStringExtra() to extract it from the intent object.
I have a sony smart watch and i'm trying to invoke a vibration intent by using the following:
Intent intentImplicit = new Intent(Control.Intents.CONTROL_VIBRATE_INTENT);
startActivity(intentImplicit);
it says startActivity its not declared in the control extension. How can I fix this?
I found this online "You can get a Context object from the Constructor for the Control and save it to a member variable then just call context.startActivity()." but i'm unsure on how to do this
The Sony SDK documentation says you should use sendBroadcast()
See: http://developer.sonymobile.com/reference/sony-addon-sdk/com/sonyericsson/extras/liveware/aef/control/Control.Intents#CONTROL_VIBRATE_INTENT
So this should work:
context.sendBroadcast(intentImplicit, Registration.HOSTAPP_PERMISSION);
If you used a sample application from Sony as base, the context is already saved as a field of your class. If not, you can get a reference in the constructor of the Extension and save it to a field like this:
public class TestExtension extends ControlExtension
{
private Context context;
TestExtension(final String hostAppPackageName, final Context context, Handler handler)
{
super(context, hostAppPackageName);
this.context = context;
}
}
What is the purpose of trying to send that Intent? If you are just trying to activate the Vibration, there is already a method built into the utilities class to do this. Take a look at the ControlExtension.startVibrator() method in the SmartExtensionUtils project.