0

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 Fragmentthere 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;

    }
}
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
Rafael
  • 1,437
  • 6
  • 23
  • 41
  • You may want to utilize a singleton pattern or something that acts as a holder for your information if you plan to switch back and forth between activities on a consistent basis. Not that this is the best solution, but it is a potential solution. I'm kind of surprised the fragment is getting gc'ed and needs to get recreated though, unless I am missing something in the code that asks for it explicitly. Those things usually stick around for a little while as your application isn't keen on losing it's stack while it is running. – zgc7009 Feb 11 '16 at 18:27
  • The fragment could be being recreated because of the activity? When I set the viewPager adapter or the tablayout? Because every time I get in the activity the fragment is recreated... I also tried using `onSaveinstanceState` but it's null every time the fragment is created or resumed. – Rafael Feb 11 '16 at 18:52
  • I've made an edit to the question, think I've found the problem, just need to make it work now. – Rafael Feb 11 '16 at 19:39
  • Ah yes that makes more sense. Look into startActivityForResult and sending date in the result intent from the called activity. – zgc7009 Feb 11 '16 at 19:41
  • Definitely do not do a singleton. – Rob Feb 11 '16 at 19:43
  • I'm gonna take a look in `startActivityForResult()` think is gonna be the answer – Rafael Feb 11 '16 at 19:44

2 Answers2

0

I think what you want is view group: Difference between View and ViewGroup in Android.

If you watched the tech event Google held for the Android Studio 2.0 preview, they mentioned specifically that they thought people were using Fragments all over the place when they should be using View Groups.

(I ran into this myself.)

Community
  • 1
  • 1
Rob
  • 11,446
  • 7
  • 39
  • 57
0

Using the tip from zgc7009 I tried using the startActivityforResult() method, which worked perfectly, so in order to make it work, on my EditText I've changed startActivity() to startActivityforResult() and on my RecyclerView instead of :

actMain.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(actMain);

I'm using:

setResult(RESULT_OK,actMain);
finish();

So, I've overrided the method onActivityResult() of my MainActivity and called onActivityResult() in my fragment to fetch the data.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Rafael
  • 1,437
  • 6
  • 23
  • 41