Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 811
kbluue Avatar asked Oct 29 '25 03:10

kbluue


1 Answers

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.

like image 98
Dani Ivanov Avatar answered Oct 31 '25 19:10

Dani Ivanov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!