kbluue
kbluue

Reputation: 379

How to manually call onSaveInstanceState()

I am writing a program where I call multiple layouts on the same activity but then i noted that when i switch layouts, the changes made before the switch are not restored and onSavedInstanceState(Bundle outState) is not called. I have tried to manually call the method but i can't get the Bundle outState.

So the question really is: How do I get and store the current state of an activity to be recalled and/or restored at a time of my choosing?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_contact_view);
// more code
}

@Override
public void onBackPressed() {
    if (layoutId == R.layout.activity_contact_view) exit();
    else if (layoutId == R.layout.main) {
        Toast.makeText(NsdChatActivity.this, "Successful back button action", Toast.LENGTH_SHORT).show();
        setContentView(R.layout.activity_contact_view);
        refreshContactList();
    }
}

And then from a seperate class

public void updateList(final int found) {
    LinearLayout layxout = (LinearLayout) ((Activity)mContext).getWindow().getDecorView().findViewById(R.id.others);
    TextView t = new TextView(mContext);
    t.setClickable(true);
    t.setText(found + ". " + activity.sNames.get(found));
    t.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    t.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //show chat view
            activity.setContentView(R.layout.main);
            TextView name = (TextView)activity.findViewById(R.id.clientName);
            name.setText(activity.sNames.get(found).split(" \\(")[0]);
            final ScrollView scroll = (ScrollView)activity.findViewById(R.id.scroll);
            scroll.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View view, boolean b) {
                    scroll.fullScroll(View.FOCUS_DOWN);
                }
            });
        }
    });
    layxout.addView(t);
}

Upvotes: 4

Views: 5320

Answers (2)

Dani Ivanov
Dani Ivanov

Reputation: 58

I might be late for that but what you could do is to keep your sate as a member of the class. That way you can restore the state anytime you want.

Bundle mState;

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
    // Save the state
    savedInstanceState = mState;
    super.onSaveInstanceState(savedInstanceState);
}
@Overrite
public void onRestoreInstanceState(Bundle savedInstance){
    mState = savedInstance;
    Restore();
}

public void Restore(){
    //access your state and restore
}

Also you shouldn't use setContentView to switch between views it's expensive way to do it. You might want to check ViewSwitcher or ViewFlipper or someway to implement Fragments.

Upvotes: 4

duan
duan

Reputation: 56

May be you should have a look at Application Fundamentals

Android calls onSaveInstanceState() before the activity becomes vulnerable to being destroyed by the system, but does not bother calling it when the instance is actually being destroyed by a user action (such as pressing the BACK key)

so you call multiple layouts on the same activity may not cause the above situation. For more details, you can refer to the question Android: Saving a state during Android lifecycle. Hope that helps!

Upvotes: 1

Related Questions