0

I have a ListView with two child elements:

-----------------
|  left | right |
-----------------

Now I have a method, that swaps these two childs:

-----------------
|  right | left |
-----------------

So the right content is on the left and the left on the right. The function looks like this:

fun swapViews(A: View, B: View) {
    val viewAx = A.x
    val viewAy = A.y
    val viewBx = B.x
    val viewBy = B.y

    A.x = viewBx
    A.y = viewBy
    B.x = viewAx
    B.y = viewAy
}

The function itself works fine, but I'm having a hard time, to swap them, when the user closes the app or just goes to app overview screen.

Currently I'm doing a check on a boolean, that is stored in the sharedprefs: Something like (pseudecode):

override fun onCreate() {
    Handler().postDelayed({
        if (swapped) {
            swapViews(viewA, viewB)
        }
    , 100)
}

(BTW I needed to add the delay, because otherwise nothing would happen at all. Does anybody know why?)

While this works, when the user closes the entire app and even removes it from recent view, it does not work, when the user switches to recent app screen and switches back to the app.

It works with two LinearViews, but not with the views inside the ListView.

I'm guessing, that android reloads the listdata onResume and therefore it overrides my swapping. But I'm not sure tough.

Now my question: Has anybody any idea how I can solve that?

For me easiest would be, to have a function like:

listview.onReload(dostuff)

so I can run my function, when android loads the data for the ListView.

(For testing I also did a text override in one of the ListViews and when switching back to the app, it was gone, so android indeed reloads the ListView)

Suleyman
  • 2,765
  • 2
  • 18
  • 31
Zoker
  • 2,020
  • 5
  • 32
  • 53
  • 1
    For this, is important to understand the activity lifecycle and how android handles data when switching between configuration changes: https://developer.android.com/guide/components/activities/activity-lifecycle#saras – Omar Silva May 14 '18 at 20:49
  • 1
    As for the reason why you need the delay, it's because `onCreate()` is called before any of your views get drawn, here's some more info: https://stackoverflow.com/questions/7733813/how-can-you-tell-when-a-layout-has-been-drawn – Omar Silva May 14 '18 at 20:54
  • But I can only save values in the onSaveInstanceState right, but not the current listview. I'm not really sure, what I should store in there. I also tried to put the whole handle function into the `onRestoreInstanceState()` function, but that does not help – Zoker May 14 '18 at 22:09
  • @OmarSilva Any idea? – Zoker May 15 '18 at 09:15
  • If you're saving your `swapped` flag to shared preferences, could you not do the following: `onResume -> fetch flag -> do swapping`? – Omar Silva May 15 '18 at 13:14
  • That's actually what I do, but the onCreate does not get called, when the user just switches into apps overview and directly back to the app. Only the list view gets reloaded, the other parts of the app don't get reloaded – Zoker May 15 '18 at 17:28

0 Answers0