I've done my due diligence and checked all the answers on SO around that, incorporated suggestions but for some reason none of the methods for save and restore are getting called.
An answer suggested that I should have save at the activity level in order for the fragment save state to be called, so here's my activity
class MyActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.my_layout)
supportActionBar?.title = "My App"
}
// not called
override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
super.onSaveInstanceState(outState, outPersistentState)
}
// not called
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
}
// not called
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
}
}
My Fragment is as follows.
class MyFragment: Fragment() {
// savedInstanceState is null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
}
// savedInstanceState is null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
}
// not getting called.
override fun onSaveInstanceState(bundle: Bundle) {
super.onSaveInstanceState(bundle)
bundle.putSerializable("users", arrayListOf()) // user array
}
}
So not sure what am I missing here for the state to be saved and restored.
How am I testing? I press back button to home screen to push the app in the background and then bring it to the foreground.
In Android, onSaveInstanceState() will be called by the system to save the current state of the activity to make sure when users resume the app they will see the activity when they left before (by calling onRestoreInstanceState() to restore the state of activity). It will be called in the following conditions:
Users move from an activity to another activity
Users press the Home key to bring the app to the background
When configuration changed, like users rotate the phone or change language, etc.
In your case, because you press the Back key, so the system knows that you want to leave the activity, so no need to save the state of that activity, that why you never see onSaveInstanceState() and onRestoreInstanceState() get called.
Related
I have an onCreate() function and an onSaveInstanceState() function in my Main Activity. I have declared a Map in my onCreate() and I want to access the keys of that Map in my onSaveInstanceState() function, so as to save them to the outState bundle.
class MainActivity: AppCombatActivity(){
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
//Want to access the iconMap here, but it is outside of the Map's scope. How do I access it?
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val iconMap = mapOf("contactlessIcon" to getDrawable(R.drawable.ic_contactless_24px), "fingerprintIcon" to getDrawable(R.drawable.ic_fingerprint_black_48dp), "codeIcon" to getDrawable(R.drawable.ic_code_24px))
}
}
I'm fairly new to Android programming, so this might be an easy fix. I want to access the iconMap in the onSaveInstanceState() but it is outside iconMap's scope. I cannot make iconMap a global variable, for this crashes my app.
As #ianhanniballake commented - you shouldn't be saving Drawables that are always the same. In this case it's especially unnecessary since you're onCreate will be called again whenever the activity is restored.
But, to answer your question, you can do it like this :
class MainActivity: AppCombatActivity(){
// define it here, so it's a member of the class
lateinit val iconMap: Map<String, Drawable>
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
//now you can use it here
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//set its value
iconMap = mapOf("contactlessIcon" to getDrawable(R.drawable.ic_contactless_24px), "fingerprintIcon" to getDrawable(R.drawable.ic_fingerprint_black_48dp), "codeIcon" to getDrawable(R.drawable.ic_code_24px))
}
}
I use navigation component for navigating between my fragments. My app is simple. It has two fragmentsm the first one is list of items and the second one shows details of that Item. When user click on an item, I call
view.findNavController()
.navigate(R.id.action_photosFragment_to_photoDetailsFragment, bundle)
but problem is when I press back button, the first fragment reloads and make a network call again.
override fun onActivityCreated(savedInstanceState: Bundle?) {
photosViewModel.getPhotos()
photosViewModel.photosLiveData.observe(viewLifecycleOwner, photosObserver)
photosViewModel.loadingLiveData.observe(viewLifecycleOwner, loadingObserver)
super.onActivityCreated(savedInstanceState)
}
this block of code calls again! How can I stop reloading?
Use onCreate() in your fragment to call the network:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
photosViewModel.getPhotos()
}
I have a simple case of Activity1 -> Activity2.
In the past when I've used startActivity(Intent(this, Activity2::class.java)) there have been no issues and the onCreate() method of Activity2 would be called.
In my current case this is not happening. I have logs in the onCreate() method and they are never hit. But if I create a onStart() method it enters there. However, never in my logs for the lifetime of the application does onCreate() of Activity2 ever get hit. How is this possible. onCreate is a requirement before onStart I thought.
Here is the actual code I'm referencing above.
class Activity1 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Timber.d("onCreate")
setContentView(R.layout.activity_splash)
startActivity(Activity2.getIntent(this))
}
}
class Activity2 : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?, persistentState: PersistableBundle?) {
super.onCreate(savedInstanceState, persistentState)
Timber.d("onCreate") // Never gets touched
}
override fun onStart() {
super.onStart()
Timber.d("onStart"); // Is hit with no problems.
}
companion object {
fun getIntent(#NonNull context: Context) : Intent {
return Intent(context, Activity2::class.java)
}
}
}
You overrode the wrong onCreate - you do not want to use the PersistableBundle version. Change your onCreate to only take the savedInstanceState: Bundle? parameter:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Timber.d("onCreate") // Now it'll be called
}
I have made an app in kotlin through the android studio, Now I have used ViewModels to save UI data while phone rotation(configuration change), i also used onSaveInstanceState to save data while pressing back button but it's not working.
The code is below
fragOne.kt
class fragOne : Fragment() {
private lateinit var viewModel: fragViewModel
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
if(savedInstanceState!=null){
with(savedInstanceState) {
viewModel.num=getInt("number")
}
}
// Inflate the layout for this fragment
var binding = DataBindingUtil.inflate<FragmentFragoneBinding>(
inflater,
R.layout.fragment_fragone,
container,
false
)
viewModel = ViewModelProviders.of(this).get(fragViewModel::class.java)
// function to update number
fun updateNumber()
{
binding.number.text="${viewModel.num}"
}
updateNumber()
// setting on Click listener for add button
binding.add.setOnClickListener()
{
viewModel.addFive()
updateNumber()
}
// setting on on Click Listener for minus button
binding.minus.setOnClickListener()
{
viewModel.minusOne()
updateNumber()
}
return binding.root
}
override fun onSaveInstanceState(outState: Bundle) {
// Save the user's current game state
outState?.run {
putInt("number",viewModel.num)
}
// Always call the superclass so it can save the view hierarchy state
if (outState != null) {
super.onSaveInstanceState(outState)
}
}
}
ViewModelclass
class fragViewModel:ViewModel()
{
// Initializing num=0
var num=0
// Functions to add five or subtract one
fun addFive()
{
num=num+5
}
fun minusOne()
{
num=num-1
}
}
please tell me because data is not saved when I press back
You can override onBackPressed to do your state saving:
How to implement onBackPressed() in Fragments?
Remember to call super, so that is does also do the back command!
You could also do like the below:
// This callback will only be called when MyFragment is at least Started.
val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
// Handle the back button event
}
Really good read: https://developer.android.com/guide/navigation/navigation-custom-back
Back navigation is how users move backward through the history of screens they previously visited. All Android devices provide a Back button for this type of navigation, so you should not add a Back button to your app’s UI. Depending on the user’s Android device, this button might be a physical button or a software button.
Ref:
How to show warning message when back button is pressed in fragments
Example:
Ensure your Activity extends AppCompatActivity
class MyFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = ViewModelProviders.of(this).get(fragViewModel::class.java)
val prefs = activity.getSharedPreferences("Key")
int num = prefs.get("number", -999)
if(num != -999) {
viewModel.num = num
}
val callback = requireActivity().onBackPressedDispatcher.addCallback(this) {
prefs.edit().putInt("number", viewModel.num).apply()
}
}
...
}
I have an activity using fragments. To communicate from the fragment to the activity, I use interfaces. Here is the simplified code:
Activity:
class HomeActivity : AppCompatActivity(), DiaryFragment.IAddEntryClickedListener, DiaryFragment.IDeleteClickedListener {
override fun onAddEntryClicked() {
//DO something
}
override fun onEntryDeleteClicked(isDeleteSet: Boolean) {
//Do something
}
private val diaryFragment: DiaryFragment = DiaryFragment()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
diaryFragment.setOnEntryClickedListener(this)
diaryFragment.setOnDeleteClickedListener(this)
supportFragmentManager.beginTransaction().replace(R.id.content_frame, diaryFragment)
}
}
The fragment:
class DiaryFragment: Fragment() {
private var onEntryClickedListener: IAddEntryClickedListener? = null
private var onDeleteClickedListener: IDeleteClickedListener? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view: View = inflater.inflate(R.layout.fragment_diary, container, false)
//Some user interaction
onDeleteClickedListener!!.onEntryDeleteClicked()
onDeleteClickedListener!!.onEntryDeleteClicked()
return view
}
interface IAddEntryClickedListener {
fun onAddEntryClicked()
}
interface IDeleteClickedListener {
fun onEntryDeleteClicked()
}
fun setOnEntryClickedListener(listener: IAddEntryClickedListener) {
onEntryClickedListener = listener
}
fun setOnDeleteClickedListener(listener: IDeleteClickedListener) {
onDeleteClickedListener = listener
}
}
This works, but when the fragment is active and the orientation changes from portrait to landscape or otherwise, the listeners are null. I can't put them to the savedInstanceState, or can I somehow? Or is there another way to solve that problem?
Your Problem:
When you switch orientation, the system saves and restores the state of fragments for you. However, you are not accounting for this in your code and you are actually ending up with two (!!) instances of the fragment - one that the system restores (WITHOUT the listeners) and the one you create yourself. When you observe that the fragment's listeners are null, it's because the instance that has been restored for you has not has its listeners reset.
The Solution
First, read the docs on how you should structure your code.
Then update your code to something like this:
class HomeActivity : AppCompatActivity(), DiaryFragment.IAddEntryClickedListener, DiaryFragment.IDeleteClickedListener {
override fun onAddEntryClicked() {
//DO something
}
override fun onEntryDeleteClicked(isDeleteSet: Boolean) {
//Do something
}
// DO NOT create new instance - only if starting from scratch
private lateinit val diaryFragment: DiaryFragment
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
// Null state bundle means fresh activity - create the fragment
if (savedInstanceState == null) {
diaryFragment = DiaryFragment()
supportFragmentManager.beginTransaction().replace(R.id.content_frame, diaryFragment)
}
else { // We are being restarted from state - the system will have
// restored the fragment for us, just find the reference
diaryFragment = supportFragmentManager().findFragment(R.id.content_frame)
}
// Now you can access the ONE fragment and set the listener on it
diaryFragment.setOnEntryClickedListener(this)
diaryFragment.setOnDeleteClickedListener(this)
}
}
Hope that helps!
the short answer without you rewriting your code is you have to restore listeners on activiy resume, and you "should" remove them when you detect activity losing focus. The activity view is completely destroyed and redrawn on rotate so naturally there will be no events on brand new objects.
When you rotate, "onDestroy" is called before anything else happens. When it's being rebuilt, "onCreate" is called. (see https://developer.android.com/guide/topics/resources/runtime-changes)
One of the reasons it's done this way is there is nothing forcing you to even use the same layout after rotating. There could be different controls.
All you really need to do is make sure that your event hooks are assigned in OnCreate.
See this question's answers for an example of event assigning in oncreate.
onSaveInstanceState not working