I'm new to fragments and currently struggling with a setback.
I've got this TabLayout
in my activity, inside of it, two Fragments
. My problem is within the first one.
On it there is a EditText
, that when clicked opens a new Activity
which contains a RecyclerView
on it, when I click on one of the items of the RecyclerView
, I return to my first Activity
, and then fill the EditText
with the data from the item of the RecyclerView
. The setback is, on the Fragment
there is one more EditText
that if filled gets clear if I click on the fist one to select another item on the RecyclerView activity.
The value of the second EditText
gets clear because the fragment is being created again, how can I save it so it won't change when the activity of the fragment gets resumed and the fragment recreated?
Edit: I think I've found the root of the problem, in my RecyclerView Activity, when I click on one item, I'm returning to the activity with a new Intent
, adding the name variable using the putExtra()
method of the Intent
class, so it's restarting my MainActivity, making it recreate the fragment and so on...
The problem is that I'm already using addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
on the intent to return to the Main Activity, is there another way to return to the activity instead of recreating it?
Code snippet from where I return to the activity:
Intent actMain = new Intent(SecondActivity.this, MainActivity.class);
actMain.putExtra("NAME_SELECTED", nameSelected);
actMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(actMain);
finish();
My Activity with the tabLayout:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
fragmentOne = new FragmentOne();
tabLayout = (TabLayout)findViewById(R.id.tabLayout);
viewPager = (ViewPager)findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
// Adds the fragment to the viewpager and set's it's title
viewPagerAdapter.addFragment(fragmentOne, "FIRST"); // `fragmentOne` should be created in `FragmentPagerAdapter.getItem()`
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
@Override
protected void onResume() {
super.onResume();
// Bundle that receives the data from the activity with the RecyclerView
Bundle bundleReceived = getIntent().getExtras();
Intent intentReceived = getIntent();
// Bundle who sends the data to the fragment
Bundle fbundle = new Bundle();
// Variable send to the fragment
String name = "";
// Check if the bundle has the variable
if (bundle != null && bundle.containsKey("NAME_SELECTED")){
name = intentReceived.getStringExtra("NAME_SELECTED");
fbundle.putString("NAME_SELECTED_TO_FRAMENT",name);
// Sets the arguments to the fragment
fragmentOne.setArguments(fbundle);
}
}
My fragment:
public class FragmentOne extends Fragment implements View.OnClickListener {
Context context;
Bundle bundle;
private TextInputLayout txtinputLayoutCliente, txtinputLayoutPedidoObs;
public FragmentOne() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_one, container, false);
edtName = (EditText) view.findViewById(R.id.edtInputName);
edtCity = (EditText) view.findViewById(R.id.edtInputCity)
// Custom listener to open the second activity when the edittext is cliced
CustomListener customListener = new CustomListener();
edtName.setOnClickListener(customListener);
edtName.setOnFocusChangeListener(customListener);
// Bundle that receives the arguments
bundle = this.getArguments();
// Check if the bundle isn't empty
if (bundle != null && bundle.containsKey("NAME_SELECTED_TO_FRAMENT")) {
String nameSelected = bundle.getString("NAME_SELECTED_TO_FRAMENT")
// Sets the name on the editText
edtName.setText(nameSelected)
}
return view;
}
}