0

I am trying to get the data from previous Activity to a Fragment. I am able to get the data without Fragment but, when I put the code inside Fragment, it doesn't work. My Fragment is inside description class. Here is my code:

This is how I am sending data from previous Activity:

TextView textView = (TextView) view.findViewById(R.id.txt_name);
String text = textView.getText().toString();
    if(text.equals("ADA")) {
        intent = new Intent(view.getContext(), description.class);
        intent.putExtra("ADA", text);
        startActivity(intent);
     }

This is how I am getting it inside Fragment:

public static class DummyFragment extends Fragment {
    int color;
    TextView mTextview;

    public DummyFragment() {
    }

    @SuppressLint("ValidFragment")
    public DummyFragment(int color) {
        this.color = color;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dummy_fragment, container, false);

        final FrameLayout frameLayout = (FrameLayout) view.findViewById(R.id.dummyfrag_bg);
        frameLayout.setBackgroundColor(color);

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.dummyfrag_scrollableview);

        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity().getBaseContext());
        recyclerView.setLayoutManager(linearLayoutManager);
        recyclerView.setHasFixedSize(true);

        mTextview = (TextView) view.findViewById(R.id.textView1);
        mTextview.setText(getActivity().getIntent().getStringExtra("ADA"));

        return view;
    }
}

This is my xml code:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:id="@+id/dummyfrag_bg"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:background="@color/bg_light">

<android.support.v7.widget.RecyclerView
    android:id="@+id/dummyfrag_scrollableview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    android:paddingBottom="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_horizontal_margin" />
</FrameLayout>

And:

<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">

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="42dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Achal
  • 133
  • 2
  • 12

2 Answers2

0
TextView textView = (TextView) view.findViewById(R.id.txt_name);
String text = textView.getText().toString();
if(text.equals("ADA")) {
   DummyFragment fragment = new DummyFragment();
   Bundle bundle = new Bundle();
   bundle.putString("ADA",text);
   fragment.setArguments(bundle);
   getSupportFragmentManager()
         .beginTransaction()
         .replace(fragment,R.id.layout)
         .addTobackStack(null)
         .commit();
}

then in your fragment

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dummy_fragment, container, false);
        String text = getArguments().getString("ADA");

}

EDIT

Explaination

1) fragmentTransaction.replace(int containerViewId, Fragment fragment, String tag)

Replace an existing fragment that was added to a container. This is essentially the same as calling remove(Fragment) for all currently added fragments that were added with the same containerViewId and then add(int, Fragment, String) with the same arguments given here.

2) fragmentTransaction.add(int containerViewId, Fragment fragment, String tag)

Add a fragment to the activity state. This fragment may optionally also have its view (if Fragment.onCreateView returns non-null) into a container view of the activity.

ZeroOne
  • 8,996
  • 4
  • 27
  • 45
0

Use SharedPreferences to store the text. Do this in the Activity before loading fragment

    SharedPreferences sharedPreferences = getSharedPreferences("TextHolder", 0);
    SharedPreferences.Editor editor = sharedPreferences.edit();
    editor.putString("ADA", textView.getText().toString()).apply();

Inside the Fragment get the text from SharedPreferences like this

String textHolder = getActivity().getSharedPreferences("TextHolder", 0).getString("ADA", "");

Now set the textHolder String to the TextView of your choice inside the Fragment