Since the Android system uses the default no-arg constructor when recreating a fragment, we should not pass values over an overloaded constructor with arguments when we instantiate it. Otherwise we get a warning that says “Avoid non-default constructors in fragments: use a default constructor plus Fragment setArguments(Bundle) instead” or we even get an InstantiationException.
The correct way to instantiate a new fragment and passing values to it, is by creating a bundle, setting arguments on this bundle and then passing it to our fragment. Inside the fragment we then retrieve this bundle and get the values out of it. A clean way to do this is by creating a so called “factory method”.
In MainActivity.java class add these lines of codes:-
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ExampleFragment fragment = ExampleFragment.newInstance("example text ", 123);
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
}
}
In activity_main.xml add these lines of codes:-
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" />
In ExampleFragment.java class file add these lines of codes:-
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class ExampleFragment extends Fragment {
private static final String ARG_TEXT = "argText";
private static final String ARG_NUMBER = "argNumber";
private String text;
private int number;
public static ExampleFragment newInstance(String text, int number) {
ExampleFragment fragment = new ExampleFragment();
Bundle args = new Bundle();
args.putString(ARG_TEXT, text);
args.putInt(ARG_NUMBER, number);
fragment.setArguments(args);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.example_fragment, container, false);
TextView textView = v.findViewById(R.id.text_view_fragment);
if (getArguments() != null) {
text = getArguments().getString(ARG_TEXT);
number = getArguments().getInt(ARG_NUMBER);
}
textView.setText(text + number);
return v;
}
}
In example_fragment.xml add these lines of codes:-
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/holo_green_light"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="@+id/text_view_fragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment Text"
android:textSize="20sp" />
</LinearLayout>

Comments