user9480759
user9480759

Reputation: 17

newInstance(); recieves null in the fragment

Good Day.

I have a problem when it comes to sending variable data from Activity to Fragment.

Here is my code in

Activity.java

    if (savedInstanceState == null) {
        Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no);
        getSupportFragmentManager().beginTransaction().replace(R.id.voter_fragment,
                new Fragment_Voter_Home()).commit();
        navigationView.setCheckedItem(R.id.voter_home);
    }

then in

Fragment.java

public class Fragment_Voter_Home extends Fragment {

private static final String ARG_STUDNO = "stud_no";

private String student_no;

public static Fragment_Voter_Home newInstance(String student_no) {

    Fragment_Voter_Home fragment = new Fragment_Voter_Home();
    Bundle args = new Bundle();
    args.putString(ARG_STUDNO, student_no);
    fragment.setArguments(args);
    return fragment;
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {


    View v = inflater.inflate(R.layout.fragment_voter_home, container, false);
    TextView textView = v.findViewById(R.id.voter_fragment_textview);

    if (getArguments() != null) {
        student_no = getArguments().getString(ARG_STUDNO);
    }

    textView.setText("Welcome, "+student_no);

    return v;

}
}

This is my Fragment.xml

<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white">

<TextView
    android:id="@+id/voter_fragment_textview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:layout_marginBottom="8dp"
    android:layout_marginEnd="8dp"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:gravity="center_vertical|center_horizontal|center"
    android:text="Home"
    android:textSize="30sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />
    </FrameLayout>

Then after running this is what I get,

"Welcome, null"

I already tried the solutions in this thread Send a string value from Activity to Fragment in Android but none of them works.

I am new to android development. Thank you.

Upvotes: 0

Views: 1251

Answers (2)

Jojo Narte
Jojo Narte

Reputation: 3104

What's wrong is you instantiated the fragment like this:

Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no);

Then you didn't pass it into the FragmentTransaction, instead you passed a new Fragment_Voter_Home().

This makes your instantiated fragment which contain the passed argument unused then what you passed into the fragment stack contains uninitialized arguments which resulted in null.

Upvotes: 0

Son Truong
Son Truong

Reputation: 14173

Because your code is not correct, change your code to

Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no);
getSupportFragmentManager().beginTransaction().replace(R.id.voter_fragment,
                fragment).commit();

instead of

Fragment_Voter_Home fragment = Fragment_Voter_Home.newInstance(Pstud_no);
getSupportFragmentManager().beginTransaction().replace(R.id.voter_fragment,
                new Fragment_Voter_Home()).commit();

Upvotes: 3

Related Questions