How to Refresh a Fragment in Android Studio(2021)

How to Refresh a Fragment in Android Studio(2021)


Introduction 

Hello knowledge seekers, today we are going to learn how to refresh a fragment.
Refreshing a fragment is a very underrated topic which usually used by many of us but not fully explained anywhere.

So as we know we can refresh a activity by using finish() and startActivity() again.

Fragment is quite different, the main idea of refreshing a fragment is to deatach  it from our base activity and instantly attach it .

Let's start with adding our fragment to our base activity : 

Main Activity xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<FrameLayout
android:layout_width="match_parent"
android:id="@+id/frame_ly"
android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Main Activity Java

public class MainActivity extends AppCompatActivity {


FragmentManager fragmentManager;
fragment_sample fragment_sample = new fragment_sample();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.add(R.id.frame_ly,fragment_sample)
.show(fragment_sample)
.commit();
}
}
Here fragment_sample is the name of our fragment.

So with these steps we have easily added our fragment. It's time to play with the fragment.


Fragement xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".fragment_sample">

<ImageView
android:layout_width="100dp"
android:layout_centerInParent="true"
android:src="@drawable/watermark"
android:id="@+id/img"
android:layout_height="100dp"/>

<Button
android:layout_width="wrap_content"
android:id="@+id/btn_reload"
android:layout_centerHorizontal="true"
android:text="Reload"
android:layout_below="@+id/img"
android:layout_marginTop="50dp"
android:layout_height="wrap_content"/>

</RelativeLayout>

Fragment Java

public class fragment_sample extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_sample, container, false);


Toast.makeText(getContext(), "Reloaded", Toast.LENGTH_SHORT).show();
view.findViewById(R.id.btn_reload).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getFragmentManager().beginTransaction().detach(fragment_sample.this).attach(fragment_sample.this).commit();
}
});

return view;
}
}

So as discussed above we have firstly detached our fragment and again attached it.

This was the method to reload the fragment .
In case, we you need to reload our fragment from activity please comment below we'll make a dedicated tutorial for this also.

Feel free to ask doubts in comment section



Post a Comment

0 Comments