does Saved instance state on onCreate and onActivityCreated of a fragment differs? - android

#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.savedInstanceState = savedInstance;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
try {
Log.d("Equals?",savedInstanceState.equals(this.savedInstanceState));
} catch (Exception e) {
Log.d("Equals?",savedInstanceState==this.savedInstanceState);
}
}
Will this always\never or only on some cases log true?
EDIT:
after seeing Henry's comment, lets address my question as if Intent has overriden its equals and it does compare those objects content, not by references...

onCreate(Bundle) called to do initial creation of the fragment.
onActivityCreated(Bundle) tells the fragment that its activity has completed its own Activity.onCreate().
so we can say that Bundle savedInstanceState are same at both places.
for more visit
http://developer.android.com/reference/android/app/Fragment.html
http://developer.android.com/guide/components/fragments.html
you wrote
#Override
public void onCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
this.savedInstanceState = savedInstance;
}
i do not understand which lifecycle mehod is onCreated i think it should be onCreate and the lines should be
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
....}

Related

How to save fragment state with annotations?

I have a little project, one activity and two fragment. I have started from the "Bottom Navigation Activity" sample on a new projet.
The I use the Android Annotations (https://github.com/androidannotations/androidannotations/wiki) for injecting my fragment, It display correctly but when I rotate, I loose all my information and the app display the first fragment, even if I displayed the second one. I tried to save my data, from an edit text with a Bundle object, in the "onSaveInstanceState" method, I get back it in the "onActivityCreated" or in the "onCreate" method but after their method were re-call and my data desappear.
I don't know how to save which fragment was displayed and how re-displayed it with the android annotations.
In my Activity class I have those methods :
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
And in my main fragments :
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if(dpdEditText != null){
rttNumberTemp = rttEditText.getText().toString();
outState.putString("rttNumberTemp", rttNumberTemp);
}
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.d(Tag, "onActivityCreated");
if(savedInstanceState != null){
Log.d(Tag, "onActivityCreated if");
updateEditText(rttEditText,savedInstanceState.getString("rttNumberTemp"));
}
}
So my question is how can I save my fragment, and its data and which fragment is displayed ?
I finally find my mistake. In my main activity I have the following method :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
So the program call the view by the injection and call it again with the setContentView. So I update the method like that :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

Save Instance Variable values through Android Activity Life Cycle

I use an intent to start another activity to display data in an ArrayList.
After pressing the back button in the action bar, the values of the array list are lost.
How do I fix this?
The code I'm currently using:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putStringArrayList(X_COORDINATE, dataX);
savedInstanceState.putStringArrayList(Y_COORDINATE, dataX);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
dataX=savedInstanceState.getStringArrayList(X_COORDINATE);
dataY=savedInstanceState.getStringArrayList(Y_COORDINATE);
}
The first method is how I put my arraylists in to the Bundle, and the second method is how I restore it.
try this:
protected void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putStringArrayList(X_COORDINATE, dataX);
}
And restore the values in onCreate():
public void onCreate(Bundle savedInstanceState) {
if (savedInstanceState!= null){
dataX=savedInstanceState.getStringArrayList(X_COORDINATE);
}
}

How to call a method in Android from onCreate

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chustilla();}
public void chustilla (View v){ //Do anything }
When I compile it gives me a problem in the parameter of chustilla(). What can I do to call this method from onCreate?
PD: If I put "this" or "null" inside the brackets it doesn't work aswell
chustilla(View) requires a View reference to be passed as a parameter so it won't work if you don't pass a View reference to it. According to what is done inchustilla(View) (best known to you) you can pass it a View from the layout (Also, best known to you).
Your method public void chustilla (View v) expects to be passed a View object. But in onCreate() You simply call chustilla(). You need pass it a view object.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chustilla(a);
}
public void chustilla (Int a)
{
//your code
}
Try the following:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chustilla();
}
public void chustilla ()
{
//Do anything
}
or use this for example:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String s;
chustilla(s);
}
public void chustilla (String v)
{ //write code here }
example parameter "new View(this)" :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
chustilla(new View(this));}
public void chustilla (View v){ //Do anything }

Android fragment state always null in onCreateView method and onActivityCreated method

I have used multiple tabs in fragment class. I was failed in saving the state of fragment. I used the method onSaveInstanceState() and save a parameter in it. But when I came back to this fragment the saved state always shows "null". I tried all the questions and answers regarding to this.Still I am facing the same problem. Please could anyone help me to solve this.
MY CODE
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setRetainInstance(true);
super.onCreate(savedInstanceState);
Log.d("savedInstanceStateInfo1", "" + savedInstanceState);
if (null != savedInstanceState) {
nAndroids = savedInstanceState.getInt("nAndroids");
Log.d("NANDROIDS", "" + nAndroids);
}
}
// Log.d("State in oncreate : ",""+savedInstanceState);
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
Log.d("SavedinstanestateInfo", "" + savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
if (container == null) {
return null;
}
// final ActionBar ab=getActivity().getSupportActionBar();
View v = (LinearLayout) inflater.inflate(R.layout.infotab, container, false);
Log.d("SavedinstanestateInfo", "" + savedInstanceState);
return v;
}
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putInt("nAndroids", nAndroids);
super.onSaveInstanceState(outState);
}
1) Apparently when using setRetainInstance(true), the Bundle for onRestoreInstanceState is null.
https://stackoverflow.com/a/8488107/1011746
2) Not necessary, but more customary to call super.onSaveInstanceState(outState) before making any modifications to outState. Same for super.onRestoreInstanceState(outState) in onRestoreInstanceState(Bundle outState).

ListFragment setRetainInstance (true); does not work

I use ListFragment
setRetainInstance (true); does not work, I do not know why
savedInstanceState == null
my sources
public class Fragment_Left extends ListFragment {
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.d(LOG_TAG, "Fragment1 onAttach");
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.d(LOG_TAG, "Fragment1 onCreate");
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
getListAdapter().getItem(position);
}
public void onSaveInstanceState(Bundle state) {
super.onSaveInstanceState(state);
Log.i(LOG_TAG, "onSaveInstanceState()");
}
}
public class Fragment_Left extends ListFragment {
private ArrayList<Menu_item> menu_list; .....
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setRetainInstance(true);
menu_list = new ArrayList<Menu_item>();......
but After I turned Screen menu_list is null, or I did something wrong?
how do I store an array in a fragment?
I am assuming that by "store an array in a fragment" that you mean "retain an array used by a fragment across configuration changes". If so:
Option #1: Put it in a data member of the fragment, and call setRetainInstance(true) on that fragment.
Option #2: If the array is of a data type that is supported by Bundle, override onSaveInstanceState(), put the data in the Bundle, and retrieve that data in other methods in the new fragment instance created after the configuration change (e.g., onCreateView()).
Option #3: If the array really represents the data model of your app, hold it in a central persistent spot that the old and new fragment instances can access, such as a database or a file.

Categories

Resources