خرید بک لینک

Vote count: 0

I want to generate a new Fragment with the next object in a LinkedList.

When a user clicks next I want to reassign a global reference to the next one in the LinkedList and create a fragment with it.

Problem

Each time the user clicks next, the object will only be reassigned to the 2nd object in the LinkedList even if there is 5 objects in the LinkedList ie. .get(1)

Possible cause I am thinking

Because I get the first object in my LinkedList in the Activity onCreate

parentFlow.getChildElements().get(0)

When the user clicks Next, the global reference is set to .get(1) in the Linkedlist, but when .newInstance() method gets called, the parent activity onCreatecalled again?

(reassigning the global reference back to .get(0) causing an endless cycle)

Is this the correct mechanism of fragment creation? (ie. Activity.onCreate() --> Fragment.onCreate --> Activity.onCreate())?

Code,

FlowStateActivity.java

public class FlowStateActivity extends AppCompatActivity
        implements FlowElementFragment.OnFragmentSelectedListener {

    private static final String TAG = FlowStateActivity.class.getName();

    private Flow parentFlow;
       // Parent Object Containing the LinkedList
    private FlowElement fe;
      // Global reference used to reassign object to next object in LinkedList

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flow_state);

        parentFlow = getIntent().getParcelableExtra("parent");
            // Gets the parent object which contains the LinkedList

            fe = parentFlow.getChildElements().get(0);
                // Assigns fe to the first element in the LinkedList

            FlowElementFragment fragment = FlowElementFragment.newInstance(
                   fe
            );

            // Replace whatever is in the fragment_container view with this fragment,
            FragmentTransaction transaction = getFragmentManager().beginTransaction();

            transaction.add(R.id.flowstate_fragment_container, fragment)
                    .commit();

        }

    @Override
        // User selects NEXT inside the Fragment 
    public void onNextSelected(View v) {
        try {
            fe = parentFlow.getChildElements()
                    .get(
                            fe.getNext()
                    );
                // Assigns global reference to next element in the LinkedList

            FlowElementFragment fgt = FlowElementFragment.newInstance(fe);

            FragmentTransaction transaction = getFragmentManager().beginTransaction();

            transaction.replace(R.id.flowstate_fragment_container, fgt)
                    .commit();
        } catch (IndexOutOfBoundsException e) {

        }


    }

FlowElementFragment.java

 public FlowElementFragment extends Fragment {
    public static FlowElementFragment newInstance(FlowElement e) {
        Bundle args = new Bundle();
        args.putParcelable(FLOW_ELEMENT, e);
        FlowElementFragment fragment = new FlowElementFragment();
        fragment.setArguments(args);
        retu fragment;
    }
 }

asked 1 min ago

برچسب: نویسنده: استخدام کار تاريخ: دوشنبه 4 مرداد 1395 ساعت: 5:12

صفحه بندی