Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fragment state loss?

What exactly is a "State Loss" situation? can you demonstrate a full scenario(include some code) which will cause it? I have read many tutorials(official and unofficial), blogs, stackoverflow questions/answers about the topic but never so an example that will cause an actual state loss and what actualy happens after the state loss occur.

I don't know the full steps to do in order to cause a state loss. What I tried to do is:

  1. An Activity which have a layout that contains a frame layout.
  2. A Fragment which have a layout with an 'hello' text inside some TextView.
  3. The Fragment is added with a fragment transaction to the above frame layout.
  4. A scenario which the user press the home button and then a fragment transaction occurs which is commited using commitAllowingStateLoss() method.

Then I tried to do a configuration change but everything was ok. Then I tried to just resume from background but also everything was ok. Then I tried to a add the fragment to the back stack and do the commitAllowingStateLoss() and then the above steps ,but when the configuration change occur or when I came back from background, and then I pressed the back button which did remove the transaction, meaning removing the fragment from the back stack which means no state loss occur.

So what is exactly that 'state loss'? how its affect what the user will see on the screen. can you show a full state loss scenario?

EDIT: put some test code:

Activity code:

package com.example.statelosstest;

import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.os.Handler;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
}

@Override
protected void onStop() {
    super.onStop();

    //Just for test
    Handler handler = new Handler();
    handler.postDelayed(new Runnable() {

        @Override
        public void run() {
            addDummyFragment();             
        }
    }, 5000);
}

/**
 * Invoke this method after onSaveInstanceState has been called,
 * for example after user press android home button.
 */
private void addDummyFragment(){
    DummyFragment frag = new DummyFragment();
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.add(R.id.fragment_frame, frag, frag.getClass().getName());

    // tried this also
    // transaction.addToBackStack(frag.getClass().getName());

    transaction.commitAllowingStateLoss();
}

}

EDIT: STILL NO ANSWER -
I tried to simulate things which I thought will cause a state loss and that I will see that state loss in the UI but couldn't simulate or cause any state loss situation. Please can somebody show a scenario which a state loss occurs and that it can be seen in the UI, meaning that the state loss really happen the an unexpected behavior really occurred.

like image 568
Elizabeth Avatar asked Nov 06 '14 18:11

Elizabeth


1 Answers

Some state loss examples:

1. suppose u have a button and a textview. in code, u have defined integer i with initial value 0 which increments by one by cicking button and its value is displayed in the textview. suppose u have pressed button 5 times, then textview will be set to 0. That is Integer lost its value on screen change.

Solution: override onSaveInstanceState and put the value of integer. then retain the value of that integer in onResume or onCreate method.

2. Consider u have a todo listview. User add to dos in that listview dynamically. suppose user have added 3 items, then on screen rotation (or if app restarts), the listview will become empty.

Solution: Again in onSaveInstanceState, put the ArrayList. (Lists can be implemented as "Serializabe" or "Parcelable" and can be stored as a whole. U dont need to store every item of listview). then in onResume or onCreate method, get that listview.

3. All the edittexts in the screen will become empty when screen rotates.

What I understand about state loss in simple language is that whenever configuration changes (screen rotation or app restarts), the entire lifecycle of the activity is called. So, whatever was there which was not saved will be lost.

Well this reminds me of Nintendo's exit screen message, "Anything not saved will be lost".

like image 119
Hirak Chhatbar Avatar answered Sep 30 '22 19:09

Hirak Chhatbar