简体   繁体   中英

Fragment lifecycle within activity

In many websites i see that a fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle - when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments.

But, it's also written there that we can reuse fragment in different activities - but from above, if we move to another activity, the fragment will be destroyed. What i'm missing, or moreover, can someone give me an example of reuse same fragment in different activities?

I think you're getting confused with the concepts of Fragment implementation and Fragment instance. You can use the same Fragment implementation in different Activity , but for each Activity you need a new Fragment instance. The lifecycle of that instance is what will be directly affected by the host Activity 's lifecycle.

Having a Fragment , let's call it FragmentA , and a couple Activity , let's call it ActivityA and ActivityB , you have 3 classes:

public FragmentA extends Fragment {
    // All the FragmentA implementation
}

public ActivityA extends Activity {
    // All the ActivityA implementation
}

public ActivityB extends Activity {
    // All the ActivityB implementation
}

In this case, you could use the implementation of FragmentA in booth ActivityA and ActivityB , but for each case you'll need to create a new instance of FragmentA .

public ActivityA extends Activity {

    loadFragmentA() {
        FragmentA instanceA = new FragmentA();
        getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.fragment_container, instanceA)
                    .commit();
    }
}

public ActivityB extends Activity {
     loadFragmentA() {
        FragmentA instanceB = new FragmentA();
        getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.fragment_container, instanceB)
                    .commit();
    }
}

Like this, instanceA would be related to the lifecycle of ActivityA and instanceB would be related to the lifecycle of ActivityB , but booth are instances of FragmentA .

Reusing means that a certain Fragment ( FragmentA ) isn't tied to a certain Activity ( ActivityA ), but can be used by different Activities ( ActivityB , ActivityC ). It doesn't mean that you can pass a Fragment instance between Activities .

You are getting it little wrong. By reuse it means that we can use one fragment(definition) at multiple places rather than the fragment object itself. It doesn't mean that you can pass fragment instance between activities. For new activity, there is a new SupportFragmentManager/Manager. So, you have to create a new instance of the same fragment.

Fragment thus allows you to keep only one piece of code for different screens.

The key here is onAttach(Activity) method of Fragment which is called once the fragment is associated with the parent activity.

You create an instance of a fragment class for any activity you need to use the fragment in and use it in a fragment transaction.

DetailsFragment details = DetailsFragment.newInstance(index);

FragmentTransaction ft = getFragmentManager().beginTransaction();
                
ft.replace(R.id.details, details);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);

ft.commit();

If you want to reuse a fragment, you should conver the activity as a fragment.

You only can reuse a fragment of an existing fragment of the FragmentManager you want to do the transaction (of activity or fragment).

See this post from FragmentManager: https://developer.android.com/reference/android/app/FragmentManager.html The fragments are allowed in the BackStack if you call the method addToBackStack at transaction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM