4

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);
}
kbluue
  • 369
  • 1
  • 5
  • 20
  • Does switching the layout causes the Activity to be implicitly recreated? If not then `onSavedInstanceState(Bundle outState)` will not be called. – Enzokie Jun 08 '17 at 02:11
  • Forgive me but could you explain what you mean when you say `implicitly recreated`? I use `setContentView()` to switch between layouts. – kbluue Jun 08 '17 at 02:22
  • A good example will be : when you rotate the device the Activity is destroyed and recreated so that's an implicit activity recreation because you never tell to recreate the activity. – Enzokie Jun 08 '17 at 02:24
  • My app doesn't switch implicitly. I notify it to switch between layouts. Is there anyway i could con my program to switch implicitly so that i can get what i need from an overridden `onSavedInstanceState()`? – kbluue Jun 08 '17 at 02:27
  • Right I don't know what's happening in your app maybe if you can share your relevant code and explain a bit the usecase. – Enzokie Jun 08 '17 at 02:28

2 Answers2

3

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.

1

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!

duan
  • 56
  • 3
  • 1
    Was helpful but i already checked that out. What i want now is to be able to record and store current state of my activity. – kbluue Jun 08 '17 at 02:24