Have fragment on activity, and when rotation content disappears. Any idea what is wrong?
public class MenuActivity extends AppCompatActivity {
static MenuActivity menuActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
menuActivity = this;
setContentView(R.layout.menu);
App.setContext(this);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction().add(R.id.frameLayout, new MessageListFragment()).commit();
}
}
}
You must save the instance state of your fragment. You must save the state of your fragment from your activity AND from your fragment. Basically, the activity triggers the fragment to save its instance state. From the activity, you can do something like this:
Then you restore it in
onCreate
like this:If the only things you need to save are the content of
TextView
s, this should be enough. If you have variables to save for example, then you need to do something similar in the fragment. The principle for the fragment is basically the same.The difference with the fragment is that the restoring is done both in
onCreate
andonCreateView
depending on what you saved and what you want to do with the saved content.inside Fragment call this method:
Also you need to check whether View is null or not, and prevent from recreating it.