Restoring the value of activity member variable not working

63 views Asked by At

This is my scenario. I have an activity that is a directory browser. When the user taps on a file in the list of the activity, it will start another activity that will display the file contents.

The second activity has the default back button in the action bar. When I tap on this, I want the first activity to reappear and show the same directory that was open before.

How do I properly store the location and how do I restore it when the first activity becomes active again?

I tried the code below, but the savedInstanceState object in the OnCreate() method is always null and the else branch will be executed! What is the reason for this?

private String rootFolder = "/";
private String currentDirectory = rootFolder;

@Override
protected void onSaveInstanceState(Bundle savedInstanceState) {
    super.onSaveInstanceState(savedInstanceState);
    savedInstanceState.putString(getString(R.string.state_current_directory), currentDirectory);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_file_picker);
    pathDisplay = (TextView)findViewById(R.id.path);

    if (savedInstanceState != null) {
        String lastDirectory = savedInstanceState.getString(getString(R.string.state_current_directory));
        getDirectory(lastDirectory);
    }
    else {
        // THIS BRANCH WILL ALWAYS BE EXECUTED?!?
        getDirectory(currentDirectory);
    }
}
0

There are 0 answers