Skip to content

Commit

Permalink
serialization fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
JayyyR committed Dec 27, 2017
1 parent 0560b36 commit f567f49
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package com.joeracosta.library.activity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import java.util.*
import kotlin.collections.ArrayList

/**
* Created by Joe on 8/14/2017.
Expand All @@ -21,15 +22,24 @@ abstract class FragmentStackActivity : AppCompatActivity() {
super.onCreate(savedInstanceState)

savedInstanceState?.getSerializable(BACKSTACK_FRAG_TAGS)?.let {
mBackstackTags = it as Stack<String>

if (it is ArrayList<*>) {
val arrayOfTags = it as ArrayList<String>
for (tag in arrayOfTags){
mBackstackTags.push(tag)
}
}

if (!mBackstackTags.isEmpty()) {
mCurrentFragment = supportFragmentManager.findFragmentByTag(mBackstackTags.peek()) as SimpleFragment
}
}
}

override fun onSaveInstanceState(outState: Bundle) {
outState.putSerializable(BACKSTACK_FRAG_TAGS, mBackstackTags)
val arrayOfTags = ArrayList<String>() //stacks don't always serialize...lets preemptively put it in an arraylist
arrayOfTags.addAll(mBackstackTags)
outState.putSerializable(BACKSTACK_FRAG_TAGS, arrayOfTags)
super.onSaveInstanceState(outState)
}

Expand Down Expand Up @@ -82,7 +92,7 @@ abstract class FragmentStackActivity : AppCompatActivity() {
*/
protected fun handleBackPress() : Boolean{
if (mCurrentFragment?.onSimpleBackPressed() ?: false) {
return true;
return true
}
if (supportFragmentManager.backStackEntryCount > 0) {
mBackstackTags.pop()
Expand All @@ -94,9 +104,9 @@ abstract class FragmentStackActivity : AppCompatActivity() {
} else {
finish() //have to call finish to finish the activity when there's one fragment left in the stack
}
return true;
return true
}
return false;
return false
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,22 @@ abstract class FragmentStackFragment : SimpleFragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
savedInstanceState?.getSerializable(BACKSTACK_FRAG_TAGS)?.let {
mBackstackTags = it as Stack<String>
if (it is ArrayList<*>) {
val arrayOfTags = it as ArrayList<String>
for (tag in arrayOfTags){
mBackstackTags.push(tag)
}
}
if (!mBackstackTags.isEmpty()) {
mCurrentFragment = childFragmentManager.findFragmentByTag(mBackstackTags.peek()) as SimpleFragment
}
}
}

override fun onSaveInstanceState(outState: Bundle) {
outState.putSerializable(BACKSTACK_FRAG_TAGS, mBackstackTags)
val arrayOfTags = ArrayList<String>() //stacks don't always serialize...lets preemptively put it in an arraylist
arrayOfTags.addAll(mBackstackTags)
outState.putSerializable(BACKSTACK_FRAG_TAGS, arrayOfTags)
super.onSaveInstanceState(outState)
}

Expand Down

0 comments on commit f567f49

Please sign in to comment.