Aryan Rajbhandari
Aryan Rajbhandari

Reputation: 1

Static variable is empty while retrieving from fragment, working on the other fragment?

The static variable is created in the Main activity and a value is assigned to it. The value is retrieving from the historyFragment but is null when retrieving from homeFragment, which is the default fragment to load on the Main activity.Error MessageMain Activity3History FragmentHome fragment

Upvotes: 0

Views: 42

Answers (1)

Valery Logvin
Valery Logvin

Reputation: 11

Aryan It's hard to understand the execution order of your code, but I suppose when you try to assign user variable in HomeFragment (in row 51), MainActivity has no user value yet.

It's bad practice to share data between screens by static fields because the data in static fields can be removed after Activity recreation and static field will have a null value. So I can recommend you to read about MVP, MVVM, Clean Architecture, etc. As a quick solution, I can recommend you to send data from activity to fragment via Bundle.

Upvotes: 1

Related Questions