There's MvxAndroidSetupSingleton class in MvvmCross what provides several virtual methods but at the same time has private only constructor
public class MvxAndroidSetupSingleton : MvxSingleton<MvxAndroidSetupSingleton>
{
private MvxAndroidSetupSingleton()
{
}
protected virtual void CreateSetup(Context applicationContext){}
protected virtual Type FindSetupType(){}
}
I'd like to git rid of reflection-based implementation and to initialize Setup on my own, but don't see any way to do that (due to the private constructor). Is there any way to handle that?
Or at least would be glad to know reason of existing virtual methods in class with private constructor.
For the preset time there's no way to accomplish that. As I've said above, the reason I want to do that is to improve startup performance as much as possible. What I've done already is:
overrided FillValueConverters to specify Value Converters explicitly
overrided FillViewTypes and filled cache on my own.
It saved around ~1.5s on start
Update
Thanks to #CheeseBaron, private constructor was changed to protected in next commit
Related
I'm in the process of completely redesigning my Android app. Before, EVERYTHING was in the same class.
So I tried to redraw everything so that the code is clearer apart Admob than the doc advice to put in the Main thread, I separate the different part of my code in class. So I used two technique: I created a songleton that contains variables that I want to have access to constantly,and I call my classes via weak reference.
Here is what it looks like:
For example, the UIManager class that needs to update the game's IU have a weak reference looks like this:
private static SoftReference<UIManager> ManageUI;
static{ManageUI= new SoftReference<>(null);}
static UIManager get()
{
if(ManageUI.get()==null)
{
ManageUI= new SoftReference<>(new UIManager());
}
return ManageUI.get();
}
GameManager Manager=GameManager.getInstance();
to be able to use the findviewbyid for example I place in method argument the main class that is the mainthread
the singleton that contains all my variables that I want to have permanent access to looks like this:
private GameManager()
{}
/** Holder */
private static class Manager
{
/** Instance unique non préinitialisée */
private final static GameManager instance = new GameManager();
}
/** Point d'accès pour l'instance unique du singleton */
public static GameManager getInstance()
{
return Manager.instance;
}
To separate all in different class, I pass argument to my method so I can call au stuff belong to Activity like that:
(My main class is called GamePlay)
void OpenGlobalScene(GamePlay activity)
{
Manager.OnTitle=false;
if (!checkLayout(activity,R.id.globalscene)) {
LayoutInflater(activity,9, true);
LinearLayout GamePlan = (LinearLayout) activity.findViewById(R.id.globalscene);
GamePlan.setAlpha(Manager.AlphaBord);
}
}
For now, I have not noticed any problems except a few slownesses on old android phone 4.4.2.
Also compared to my old code were EVERYTHING was in the same class, it's much easier to change pieces of code (going to the inapp billing V3 was simpler since everything was in one class that I call like the others with weak referencre)
My questions are:
-What are the problems that such a structure might pose?
I had also chosen that structure to not load or leave in memory things that are not useful
-How are chance that Android will erase from memory an action in progress called with weak reference?
-As you can see I pass the activity has argument to the method, sometimes I pass it from a method to another. Is that fact can cause some trouble?
Thank you for your help.
Check Dagger2 is better than the clasic singleton https://developer.android.com/training/dependency-injection/dagger-android?hl=es-419
thanks for your answer and your tips. I'am gonna check this out.
Anyone else know something about consequences on memory when using weak references ?
I use a third-party API (JAudioTagger) and I would like to start an activity with an object of this API (AudioFile).
The problem is this object does not implement Parcelable or Serializable.
What is the best way to do this ?
EDIT
Google's answer : http://developer.android.com/guide/faq/framework.html
You have a few options, none of which are easy or perfect (depending on the object and use-case).
Create a custom object that extends the AudioFile object and implements either Serializable or Parcelable - which can be tedious, but not impossible. With custom objects like this, the documentation may be lacking for this option.
Someone mentioned static as an option. This can generally work well, except you are talking about Android. Android can destroy and re-create the JVM for your app at any time when it is not visible to the user. So, if this AudioFile class is playing in the background in your app, strange behavior could occur if Android decides to kill the process.
You can use an object in the Application class, but is potentially has the same issues as #2.
Use SharedPreferences and some kind of index system to retrieve the file.
You can create a class of your own, which would accept an object of type AudioFile, and populate fields with its values.
public class MyAudioFile implements Parcelable{
private File file;
//other fields...
public MyAudioFile(AudioFile audioFile){
this.file = audioFile.getFile();
//populate other fields
}
//parcelable stuff
}
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
What is the meaning of “this” in Java?
I'm still very new to learning Android programming, and I noticed that "this" was used often in parameters for method calls in the language. I'm following The New Boston tutorials through YouTube, but he never really explains quite detailed enough what the 'this' statement means. Can somebody please explain it to me? Maybe dumb it down a bit?
this refers to the instance of the class you are currently coding within.
You cannot use it in a static context because in this situation you are not within any object context. Therefore this doesn't exist.
public class MyClass {
public void myMethod(){
this.otherMethod(); // Here you don't need to use 'this' but it shows the concept
}
private void otherMethod(){
}
public static void myStaticMethod(){
// here you cant use 'this' as static methods don't have an instance of a class to refer to
}
}
In android class.this is used to pass context around.
Formal definition of context: It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities.
That means if you need to access resources (including R and user interface) you will have to use context.
In java this means the instance of the class that you are in. For example MainActivity.this points to the current instance of the MainActivity. So by using MainActivity.this.foo you are accessing the foo field of MainActivity class.
public class YourClass {
private int YourInt;
public setTheInt(int YourInt) {
this.YourInt = YourInt;
}
}
"this" is used to see whether an attribute or function belongs to the class we're working on, clearer.
Also, you see that setTheInt operation gets an integer named as the same as your attribute. In that function's namespace, YourInt is not this class's YourInt, but a reflection of the integer coming from setTheInt's calls. "this" helps here to divide the outer and the inner "YourInt"s.
Is there any way to access private variables of other class.Actually I am writing testCases for my library project in order to test all possible critical conditions when the app is going to crash .I shuld write in such a way that my project should pass all test cases.Now what my problem I should check for the variables which are declared as private in my library project.Is there any way to access these variables (which are declared as private).
use getters and setters ..it is the preferred way.. in setters make sure you keep safe values and make sure they will not break your code in any case..
On Android, you wanna make your activities' methods private to prevent other classes from thinking they can access it (fragment can, but that is wrong practice to me, it's better to use an observable-observer pattern). Then you will end up with private fields and methods that would need to be accessed by tests only.
BoundBox does exactly that ! Here below is an example of a test that accesses 2 private fields of an activity to test it :
#UiThreadTest
public void testCompute() {
// given
boundBoxOfMainActivity = new BoundBoxOfMainActivity(getActivity());
// when
boundBoxOfMainActivity.boundBox_getButtonMain().performClick();
// then
assertEquals("42", boundBoxOfMainActivity.boundBox_getTextViewMain().getText());
}
So, I'm working inside the android framework classes (AOSP). Since I'm technically working on Cyanogenmod, I'm supposed to set the settings for my patch in an app called CMParts, which just throws the strings and ints into the system settings via Settings.System.
Unfortunately, down in the framework (non application) code, I don't have access to a Context object (because I'm not inside an Activity or Application) to give to the usual method call to get those settings back out. Passing a null doesn't work.
Anyone know ANY way to get to those settings from framework-level-code? Passing down a context reference isn't really an option down that far...
The thing you wrote where quite close to mean something. Can you be more specific?
I do not know what framework you are using. But I'm guessing that it is something internal for your company.
But generically speaking:
if you are not aloud to change the code extend it.
if you need something from the user implementer, force the user to give it to you.
public MyNewFrameWorkClass extends AnotherFrameWorkClass {
private Context context;
// Disallow the user to create an
// instance with out giving you the context
private MyNewFrameWorkClass() {}
public MyNewFrameWorkClass(Context context) {
this.context = context;
}
}