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>