I am using a ViewPager in my app, based on a FragmentStatePagerAdapter.
I have hundreds of Fragments that are loaded and displayed dynamically in this ViewPager, all this is working well but I sometimes get null pointers when trying to access resources using:
getResources().getString(R.id.example);
What is the best way to access Resources in Fragments? Would it be a good idea to create a global variable and initialize it (maybe in onActivityCreated())?
I am also concerned about the memory itself, as I am not sure which one is the heaviest between using a global variable on every fragment or directly accessing the resources from the Activity (getResources()).
Make this a global variable
private View view;
and then on onCreateView(), add this line
view = inflater.inflate(R.layout.fragment, container, false);
and now you can access the resources
view.getResources().getString(R.string.title);
you can use:
getActivity().getResources().getString(R.id.example);
Related
I want to achieve this :
Create a ViewDataBinding by inflating a layout.
Set a tag to its root view.
Insert its root view into my layout.
Later on, call findViewWithTag() to retrieve its root view.
Getting the ViewDataBinding linked to its view.
But I can't figure out how to achieve the latest step.
This is how my code looks like :
MyViewDataBinding binding = DataBindingUtils.inflate(inflater, R.layout.my_layout, myContainer, false);
View bindingRootView = binding.getRoot();
bindingRootView.setTag("aTag");
myContainer.addView(bindingRootView);
//In another part of the code ...
MyViewDataBinding binding = myContainer.findViewByTag("aTag").getViewDataBinding();
But is their something like "getViewDataBinding"?
Thank's for help.
You can find appropriate method in DataBindingUtil class. It is called findBinding(View view). It may be needed to cast returned value to your wanted class type.
I'm not sure though whether it's the best architecture. I would be interested if you would give us more context.
I have a view which I get from the parent ViewGroup:
mActiveCard = getChildAt(LAST_OBJECT_IN_STACK);
I later what to check if mActiveCard equals to another view:
anotherCard = getChildAt(x);
A naive approach would have been to check if x== LAST_OBJECT_IN_STACK however there might be many changes in the ViewGroup e.g. removed objects. So the positions are relative.
Also I can save the object for later but that will consume some more memory, like:
mActiveCard.equals( getChildAt(LAST_OBJECT_IN_STACK) )
One idea is to setId() of the view or setTag(). So if I have a unique String/int then I could later get the id or the tag. So saving just the id/tag would require less memory, right?
First of all is my theory correct? Also, does Android SDK offer a way to identify tags and what can be an good id/tag to generate and set on that view?
If you are truly holding onto a reference to a View (mActiveCard) that you know is also in the child list, you can simply compare their references.
if (mActiveCard == getChildAt(x))
{
// ...
}
FrameLayout content = (FrameLayout) findViewById(android.R.id.content);
This gives me an error
error: cannot find symbol
FrameLayout rootLayout = (FrameLayout)findViewById(android.R.id.content);
^
symbol: method findViewById(int)
I have already imported the required R package
It seems like you are trying to access the layout of your current Activity from a different class. Instead of trying to find your FrameLayout in the different class, save the reference to the FrameLayout inside of your Activity, and pass the FrameLayout to your seperate class (the class where you are currently seeing this issue).
E.g.
Activity Class:
...
OtherObject myOtherObject = new OtherObject();
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.my_frame_layout);
myOtherObject.doStuffWithFrameLayout(frameLayout);
...
OtherObject Class:
...
public void doStuffWithFrameLayout(FrameLayout frameLayout) {
//You can use the FrameLayout here and do stuff with it.
//You will likely also want to pass in a Context object if you want to
//create a LayoutInflater or do other Context-dependent stuff
}
...
Try: FrameLayout content = (FrameLayout) findViewById(R.id.content);
In case this does not work remove the import of yourPackage.R and hit the button 'fix imports' not sure the import you did is correct. I always get 2 different options.
You can just call setContentView() again, but keep in mind that this will invalidate all of your View references, so make sure that you initialize them again.
There's almost never a reason to do this, so I would suggest you look into using Fragments, and just swap out the Fragments instead.
To use android.R.id.content, you must import android.R, not yourAppPackage.R .
And to use multiple layouts within one activity, you have to use ViewFlipper or ViewAnimator (or call setContentView multiple times, but it's resource expensive if you have huge layouts).
I want to optimize my Android application, but i don't know what is better?
First option:
public void function()
{
RelativeLayout rl = (RelativeLayout)findViewById(R.id.activity);
ImageView img = (ImageView)findViewById(R.id.image);
...
}
Second option:
RelativeLayout rl = (RelativeLayout)findViewById(R.id.activity);
ImageView img = (ImageView)findViewById(R.id.image);
public void function{
...
}
Which option use less resource? Global variables or local? (My program call this function every second)
In general, the second option is better. findViewById() can be fairly expensive if your view hierarchy is complex. It is better to call it once and store the results than to call it repeatedly.
Assuming the second option uses e.g. member variables, it won't even work. You need to call setContentView() e.g. in onCreate() before calling findViewById() and member variable initialization is performed before your onCreate() runs.
So the first one is better because it works while the other doesn't.
Other than that, at this level this smells of unnecessary micro-optimization. If you have performance issues, they are probably elsewhere.
Related: If you want to optimize findViewById() calls e.g. in an adapter where the same views are recycled over and over again, google for "android viewholder".
I'm new to Android and find it brutal (there seems to be an near infinite number of details and dependencies to remember).
Anywho, I got the TextSwitcher1 example app working, which uses ViewSwitcher. I'm assuming ViewSwitcher is the way to go, need to either display a map or a table, user can pick, and switch back and forth.
So I created my MapActivity in another application, seems to work. Next integrate into main app. So, call
View v = findViewById(R.layout.mapview);
and then
mSwitcher.addView(v);
except "v" is null. Why? Do I create the activity? But I don't want to show it yet. Is there such a call as "create activity but hide it until needed"? Or am I barking up the wrong tree?
Thanks for any insight.
The findViewById function returns a View based on an ID resource (R.id.something) for whatever view you have loaded in your activity (using setContentView(R.layout.main)). In your sample code, you're using a layout resource (R.layout.mapview). You should inflate the XML file, which will return a View that you can use to add to the ViewSwitcher.
Example Code:
LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.mapview, null);
mSwitcher.addView(v);
However, you should be able to define everything in your XML file and not have to manually add the pages to your ViewSwitcher. Here's some example code on how to do that: http://inphamousdevelopment.wordpress.com/2010/10/11/using-a-viewswitcher-in-your-android-xml-layouts/