Livedata not triggering in fragment after configuration change
After configuration change, the observer is not triggered anymore in my fragment. I really can't understand what I'm missing in the code (it is using latest androidx support libraries).
Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
FragmentManager fragMgr = getSupportFragmentManager();
itemListFragment = (ItemListFragment) fragMgr.findFragmentById(R.id.container_fragment_items);
if (itemListFragment == null) {
itemListFragment = ItemListFragment.newInstance();
fragMgr.beginTransaction()
.add(R.id.container_fragment_items, itemListFragment)
.commit();
}
...
Fragment:
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
mainViewModel.getAllItems().observe(getViewLifecycleOwner(), items -> adapter.setItems(items));
...
MainViewModel:
public class MainViewModel extends AndroidViewModel {
private MainRepository mMainRepository;
private LiveData<List<Item>> mAllItems;
public MainViewModel(Application application) {
super(application);
mMainRepository = new MainRepository(application);
mAllItems = mMainRepository.getAll();
}
public LiveData<List<Item>> getAllItems() {
return mAllItems;
}
...
add a comment |
After configuration change, the observer is not triggered anymore in my fragment. I really can't understand what I'm missing in the code (it is using latest androidx support libraries).
Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
FragmentManager fragMgr = getSupportFragmentManager();
itemListFragment = (ItemListFragment) fragMgr.findFragmentById(R.id.container_fragment_items);
if (itemListFragment == null) {
itemListFragment = ItemListFragment.newInstance();
fragMgr.beginTransaction()
.add(R.id.container_fragment_items, itemListFragment)
.commit();
}
...
Fragment:
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
mainViewModel.getAllItems().observe(getViewLifecycleOwner(), items -> adapter.setItems(items));
...
MainViewModel:
public class MainViewModel extends AndroidViewModel {
private MainRepository mMainRepository;
private LiveData<List<Item>> mAllItems;
public MainViewModel(Application application) {
super(application);
mMainRepository = new MainRepository(application);
mAllItems = mMainRepository.getAll();
}
public LiveData<List<Item>> getAllItems() {
return mAllItems;
}
...
Thatifcheck will only work if this Axctivity only shows that 1 Fragment, which typically is not the case. Otherwise I do not see anything technically wrong... unless the error is inMainRepository.
– EpicPandaForce
Jan 20 at 20:40
@EpicPandaForce Thanks it was because of thatif, but Why? Do I need that if at all? Could you please help me.
– Mahozad
Jan 20 at 21:38
Without thatif, maybe I shouldreplacethe fragment instead ofadding?
– Mahozad
Jan 20 at 21:47
actually, that if should beif(savedInstanceState == null) {. I'm surprised I didn't mention that. I personally just generally don't trustfindFragmentByIdand always find them by tag
– EpicPandaForce
Jan 20 at 22:15
add a comment |
After configuration change, the observer is not triggered anymore in my fragment. I really can't understand what I'm missing in the code (it is using latest androidx support libraries).
Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
FragmentManager fragMgr = getSupportFragmentManager();
itemListFragment = (ItemListFragment) fragMgr.findFragmentById(R.id.container_fragment_items);
if (itemListFragment == null) {
itemListFragment = ItemListFragment.newInstance();
fragMgr.beginTransaction()
.add(R.id.container_fragment_items, itemListFragment)
.commit();
}
...
Fragment:
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
mainViewModel.getAllItems().observe(getViewLifecycleOwner(), items -> adapter.setItems(items));
...
MainViewModel:
public class MainViewModel extends AndroidViewModel {
private MainRepository mMainRepository;
private LiveData<List<Item>> mAllItems;
public MainViewModel(Application application) {
super(application);
mMainRepository = new MainRepository(application);
mAllItems = mMainRepository.getAll();
}
public LiveData<List<Item>> getAllItems() {
return mAllItems;
}
...
After configuration change, the observer is not triggered anymore in my fragment. I really can't understand what I'm missing in the code (it is using latest androidx support libraries).
Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
FragmentManager fragMgr = getSupportFragmentManager();
itemListFragment = (ItemListFragment) fragMgr.findFragmentById(R.id.container_fragment_items);
if (itemListFragment == null) {
itemListFragment = ItemListFragment.newInstance();
fragMgr.beginTransaction()
.add(R.id.container_fragment_items, itemListFragment)
.commit();
}
...
Fragment:
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_item_list, container, false);
mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
mainViewModel.getAllItems().observe(getViewLifecycleOwner(), items -> adapter.setItems(items));
...
MainViewModel:
public class MainViewModel extends AndroidViewModel {
private MainRepository mMainRepository;
private LiveData<List<Item>> mAllItems;
public MainViewModel(Application application) {
super(application);
mMainRepository = new MainRepository(application);
mAllItems = mMainRepository.getAll();
}
public LiveData<List<Item>> getAllItems() {
return mAllItems;
}
...
edited Jan 20 at 21:51
Mahozad
asked Jan 20 at 12:05
MahozadMahozad
1,70451232
1,70451232
Thatifcheck will only work if this Axctivity only shows that 1 Fragment, which typically is not the case. Otherwise I do not see anything technically wrong... unless the error is inMainRepository.
– EpicPandaForce
Jan 20 at 20:40
@EpicPandaForce Thanks it was because of thatif, but Why? Do I need that if at all? Could you please help me.
– Mahozad
Jan 20 at 21:38
Without thatif, maybe I shouldreplacethe fragment instead ofadding?
– Mahozad
Jan 20 at 21:47
actually, that if should beif(savedInstanceState == null) {. I'm surprised I didn't mention that. I personally just generally don't trustfindFragmentByIdand always find them by tag
– EpicPandaForce
Jan 20 at 22:15
add a comment |
Thatifcheck will only work if this Axctivity only shows that 1 Fragment, which typically is not the case. Otherwise I do not see anything technically wrong... unless the error is inMainRepository.
– EpicPandaForce
Jan 20 at 20:40
@EpicPandaForce Thanks it was because of thatif, but Why? Do I need that if at all? Could you please help me.
– Mahozad
Jan 20 at 21:38
Without thatif, maybe I shouldreplacethe fragment instead ofadding?
– Mahozad
Jan 20 at 21:47
actually, that if should beif(savedInstanceState == null) {. I'm surprised I didn't mention that. I personally just generally don't trustfindFragmentByIdand always find them by tag
– EpicPandaForce
Jan 20 at 22:15
That
if check will only work if this Axctivity only shows that 1 Fragment, which typically is not the case. Otherwise I do not see anything technically wrong... unless the error is in MainRepository.– EpicPandaForce
Jan 20 at 20:40
That
if check will only work if this Axctivity only shows that 1 Fragment, which typically is not the case. Otherwise I do not see anything technically wrong... unless the error is in MainRepository.– EpicPandaForce
Jan 20 at 20:40
@EpicPandaForce Thanks it was because of that
if, but Why? Do I need that if at all? Could you please help me.– Mahozad
Jan 20 at 21:38
@EpicPandaForce Thanks it was because of that
if, but Why? Do I need that if at all? Could you please help me.– Mahozad
Jan 20 at 21:38
Without that
if, maybe I should replace the fragment instead of adding?– Mahozad
Jan 20 at 21:47
Without that
if, maybe I should replace the fragment instead of adding?– Mahozad
Jan 20 at 21:47
actually, that if should be
if(savedInstanceState == null) {. I'm surprised I didn't mention that. I personally just generally don't trust findFragmentById and always find them by tag– EpicPandaForce
Jan 20 at 22:15
actually, that if should be
if(savedInstanceState == null) {. I'm surprised I didn't mention that. I personally just generally don't trust findFragmentById and always find them by tag– EpicPandaForce
Jan 20 at 22:15
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276245%2flivedata-not-triggering-in-fragment-after-configuration-change%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54276245%2flivedata-not-triggering-in-fragment-after-configuration-change%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
That
ifcheck will only work if this Axctivity only shows that 1 Fragment, which typically is not the case. Otherwise I do not see anything technically wrong... unless the error is inMainRepository.– EpicPandaForce
Jan 20 at 20:40
@EpicPandaForce Thanks it was because of that
if, but Why? Do I need that if at all? Could you please help me.– Mahozad
Jan 20 at 21:38
Without that
if, maybe I shouldreplacethe fragment instead ofadding?– Mahozad
Jan 20 at 21:47
actually, that if should be
if(savedInstanceState == null) {. I'm surprised I didn't mention that. I personally just generally don't trustfindFragmentByIdand always find them by tag– EpicPandaForce
Jan 20 at 22:15