aquaflamingo
aquaflamingo

Reputation: 822

Is the Activity onCreate called with each NewInstance of a fragment inside it?

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);
        return fragment;
    }
 }

Upvotes: 0

Views: 69

Answers (3)

lionscribe
lionscribe

Reputation: 3513

Your code

fe = parentFlow.getChildElements() .get( fe.getNext() );

sounds very wrong. Probably should be

fe = fe.getNext();

or else

fe = parentFlow.getChildElements().get( ++current );

Upvotes: 1

Somayyeh
Somayyeh

Reputation: 326

No.

Fragment has its functions to manage its events:

onAttached() — Called when the fragment has been associated with the activity onCreateView() — Called to create the view for the fragment onActivityCreated() — Called when the activity’s onCreate() method has been returned

Upvotes: 1

Nongthonbam Tonthoi
Nongthonbam Tonthoi

Reputation: 12953

Is the Activity onCreate called with each NewInstance of a fragment inside it?

No.

It will be called only once.

Upvotes: 1

Related Questions