Rohan Chauhan
Rohan Chauhan

Reputation: 39

Data saving when orientation changes

I have put a condition in onCreate() method to check if there exist some previous data or not. But it is not working. please guide me. And I don't want to use onRestoreSavedState() method for this.d

public class ActivityOne extends Activity {

Use these as keys when you're saving state between reconfigurations

private static final String RESTART_KEY = "restart";
private static final String RESUME_KEY = "resume";
private static final String START_KEY = "start";
private static final String CREATE_KEY = "create";

String for LogCat documentation

private final static String TAG = "Lab-ActivityOne";
private int mCreate=0, mRestart=0, mStart=0, mResume=0;

You will need to increment these variables' values when their corresponding lifecycle methods get called.

TODO: Create variables for each of the TextViews

private TextView mTvCreate, mTvRestart, mTvStart, mTvResume;

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

TODO: Assign the appropriate TextViews to the TextView variables

    mTvCreate=(TextView)findViewById(R.id.create);
    mTvRestart=(TextView)findViewById(R.id.restart);
    mTvResume=(TextView)findViewById(R.id.resume);
    mTvStart=(TextView)findViewById(R.id.start);


    Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo);
    launchActivityTwoButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {

           Intent intent=null;
            intent = new Intent(v.getContext(),ActivityTwo.class);
        startActivity(intent);

        }
    });
    if(savedInstanceState!=null){
        mCreate=savedInstanceState.getInt(String.valueOf(mCreate));
        mRestart=savedInstanceState.getInt(String.valueOf(mRestart));
        mResume=savedInstanceState.getInt(String.valueOf(mResume));
        mStart=savedInstanceState.getInt(String.valueOf(mStart));
    }


    // Emit LogCat message
    Log.i(TAG, "Entered the onCreate() method");
        mCreate++;
        displayCounts();
}

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {

Save state information with a collection of key-value pairs 4 lines of code, one for every count variable super.onSaveInstanceState(savedInstanceState);

    savedInstanceState.putInt(String.valueOf(mCreate),mCreate);
    savedInstanceState.putInt(String.valueOf(mRestart),mRestart);
    savedInstanceState.putInt(String.valueOf(mResume),mResume);
    savedInstanceState.putInt(String.valueOf(mStart),mStart);

}

Updates the displayed counters This method expects that the counters and TextView variables use the names specified above public void displayCounts() {

    mTvCreate.setText("onCreate() calls: " + mCreate);
    mTvStart.setText("onStart() calls: " + mStart);
    mTvResume.setText("onResume() calls: " + mResume);
    mTvRestart.setText("onRestart() calls: " + mRestart);

}

}

Upvotes: 0

Views: 63

Answers (1)

Daniel Nugent
Daniel Nugent

Reputation: 43322

It looks like the problem is that you attempting to use values that have not been set yet when you try to capture the saved value.

All you need to do to fix it is to use the String constants that you have defined.

So, in onCreate(), you would have:

if(savedInstanceState!=null){
    mCreate=savedInstanceState.getInt(CREATE_KEY);
    mRestart=savedInstanceState.getInt(RESTART_KEY);
    mResume=savedInstanceState.getInt(RESUME_KEY);
    mStart=savedInstanceState.getInt(START_KEY);
}

Then, in onSaveInstanceState(), you would have:

savedInstanceState.putInt(CREATE_KEY,mCreate);
savedInstanceState.putInt(RESTART_KEY,mRestart);
savedInstanceState.putInt(RESUME_KEY,mResume);
savedInstanceState.putInt(START_KEY,mStart);

Upvotes: 1

Related Questions