Android: preserving activity/fragment state - android

If I want to keep track of an int value in an activity/fragment, is this approach incorrect:
In layout XML, have:
<TextView
android:id="#+id/int_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
On fragment's onCreateView or activity's onCreate, have the following code:
TextView intId = (TextView)getView().findViewById(R.id.int_id);
intId.setText(String.valueOf(<integer_value_to_keep_track_of>));
Then, whenever I need to use the int value later in the code, access it by doing the following:
int accessId = Integer.valueOf(((TextView)getView().findViewById(R.id.detail_column_id)).getText().toString());
Something tells me this is not the best way to preserve state. Would declaring a class member (e.g. private int accessId) and assigning that be better? Thanks!

Usually, if you are inside onCreateView() you get a reference of the Views that are part of the Fragment's View hierarchy only once when you inflate the layout. When you call infalter.inflate() a View instance is returned; the parent of the hierarchy. You can use that View's findViewById() to get a reference of the TextView.
For example:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle SavedInstanceState) { // TODO Auto-generated method stub
View view = (View) inflater.inflate(R.layout.me_layout, container,false);
TextView intId = (TextView)view.findViewById(R.id.int_id);
return view;
}
Then once you get the TextView reference you can simply do
String text = intId.getText().toString();
inside onCreateView();

Related

How do i assign view in onCreate method in an activity android

I am having the following method which passes View
private void doSomething(View view){ }
The problem am having is how do i call this view in onCreate method in an Activity, i will have to pass the view
For Example
View view;
doSomething(view)
How do is assign view/instantiate view, am using getView() but its not working
Like
view = getView()
For Example in fragments onViewCreatedMethod has an argument view which i can assign to the method when am calling it. Example below
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
doSomething(view);
}
Is it possible to do that inside onCreate method in fragment, can I parse the View arguments in onCreate method in activity
In your onCreate method, once you've called setContentView, you can use findViewById to get whatever View you want. If you want the root view for some reason, you can pass android.R.id.content.
For example:
#Override
public void onCreate(Bundle savedInstanceState) {
// Replace your_layout_id with your Activity layout ID
setContentView(R.layout.your_layout_id);
View rootView = findViewById(android.R.id.content);
// Replace with whatever ID/View type you have in your code
// Button and your_button_id are just examples
Button button = findViewById(R.id.your_button_id);
}

declaration of Widgets. In or outside of onCreateView?

whats the diffrence between this two codes:
EditText mTitleField;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_crime, parent, false);
mTitleField = (EditText)v.findViewById(R.id.crime_title);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_crime, parent, false);
EditText mTitleField = (EditText)v.findViewById(R.id.crime_title);
}
The first one has Edittext variable declared outside the onCreateView, the second one has its declaration inside onCreateView.
Does it matter where do I declare it?
Yes it does matter.
For the first scenario, you can use mTitleField with other methods also (though you will have to instatiate it separately for other methods).
For the second scenario, you have bot declared and instantiated mTitleField inside onCreateView(), so it is accessible only inside that method.
This is a basic concept of Global and Local variables in Java.
Off course it does matter, I assume for first case mTitleField is declared globally in class level. In this case you can access mTitleField as EditText in any other method in that particular class.
Ex: Suppose on click of any button you need the text enter in that EditText then you can simply do like this
String someString = mTitleField.getText().toString();
In second case mTitleField is declared locally. In this case outside onCreateView(...) you can not access mTitleField. Scope of mTitleField is in onCreateView(...).
Ex: Suppose on click of any button you need the text enter in that EditText then you have to declare that EditText again inside onClick method like this
EditText mTitleField = (EditText)v.findViewById(R.id.crime_title);
String someString = mTitleField.getText().toString();
The Difference is that In First Method it declared globally so the advantage of that is you can use global variable anywhere in whole activity While for Second method it is used as a local variable that you can use for Particular place in an activity.
A local variable is defined within the scope of a block.
A global variable is a variable that can be accessed in multiple scopes.

Handle textview state when fragment orientation changes

I have in my fragment layout an textview with default visibility state = "GONE";
In my fragment class i add some information to textview and show it. My fragment have setretaininstance = true for cursorLoader saving.
But when my orientation changes my textview always hide. I think it because:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment, null);
tvNowUsedFiltres = (TextView) view.findViewById(R.id.tvTextView);
It's changes state to "gone". But i cant save my instance state, because bundle always returns null with setretaininstance = true.
How can i save my text view state?
The setRetainInstance() does not work for widgets, because widgets are wired to the Activity (not the Fragment).
So, all you need to do is put a member boolean value in the Fragment class, and in onCreateView(), you reset the visibility parameter of that textview after getting it:
tvNowUsedFiltres = (TextView) view.findViewById(R.id.tvTextView);
if (myNewBoolean) {
tvNowUsedFiltres.setVisibility(View.VISIBLE);
}

Customize a xml layout in a fragment - Android

I am new in Android programming.
I created the main Activity of my app style google shop ussing ActionBarSherlock and a NavigationTabs, with fragments, each referencing another activity (Fragment 1 Fragment 2, etc) and each fragment inflating a layout.
However, I'm used to create layouts in xml and then customize them in java. To put a different text depending on the time of day, or according to some data in a database, giving function to buttons, etc.. But in a Fragment Class, I can not even use setContentView to work with each text or button, and set the context for using my database is giving me problems.
How I can customize a xml layout in a fragment?
Or what would be the right thing to do?
Here my Fragment:
public class Fragment1 extends SherlockFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.menu, container, false);
}
This is more simple then you think. onCreateView instanciate au returns the view for your Fragment. As you said, in a simple Activity you set (and instanciate) the view with setContentView() and then you get your Views with findViewById().
findViewById() asks for the view to return the view item that you want, you can call it from your view before returning it. Like this:
public class Fragment1 extends SherlockFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.menu, container, false);
// For example, getting a TextView
TextView tv = (TextView) v.findViewById(R.id.myTextView);
// do your job
return v;
}
so far so good, you just need to use the view you are inflating to get everything.
here is an example
View v = inflater.inflate(R.layout.menu, container, false);
Button b = (Button)v.findViewById(r.id.button1);
return v;
inside onActivityCreated you could use:
View mView = getView();
TextView textView = (TextView) view.findViewById(R.id.theIdOfTextView);
where theIdOfTextView is declared inside R.layout.menu.
getView() returns the View you inflated inside onCreateView. You use it only after onCreateView has been executed

Cannot getHeight of TextView when Fragment onCreateView

I have a Fragment which contain a TextView call as mTextView. I want to get height of mTextView after append content to it in onCreateView of Fragment like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(
R.layout.myfragment, container, false);
mTextView = (TextView) view.findViewById(R.id.myTextView);
int mHeight=mTextView.getHeight();
}
but it's always return 0 value.
Do you try to get it after
setContentView(R.layout.your_layout);
right?
If you try to get the height just after onCreate, you won't be able to get because still the window is not focused. Try like this :
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// your code here
int mHeight=mTextView.getHeight();
}
The reason the View's dimentions are 0 is because when you are querying them, the view still haven't performed the layout and measure steps. You only told the view how it would "behave" in the layout, but it still didn't calculated where to put each view.
There are a few tricks to get that size though. You can use this question as a starting point although I don't like the accepted answer's approach very much.
How to get the width and height of an android.widget.ImageView?

Categories

Resources