chanthini begam
chanthini begam

Reputation: 157

How to refresh fragment from activity in KOTLIN

In my kotlin project, I want to refresh fragments from an Actvity. When I finish Activity, it should refresh the fragment. I was thinking this would be done in onResume. So how to do refresh current fragment when I resume an activity? My fragment page contains listviews, hence it should be updated, when i finish activity.

Upvotes: 0

Views: 397

Answers (1)

Praveen Kumar
Praveen Kumar

Reputation: 339

try this

Use Swipe to refresh layout

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/swipeRefreshLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

<ListView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/listView"
        />

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

MainActivity

 class MainActivity : AppCompatActivity() {
   lateinit var swipeRefreshLayout: SwipeRefreshLayout

   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      swipeRefreshLayout = findViewById(R.id.swipeRefreshLayout)
      swipeRefreshLayout.setOnRefreshListener {
           //TODO operation
         }
      }
   }

Upvotes: 0

Related Questions