简体   繁体   English

分割活动中的生命周期

[英]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. 我认为您对Fragment实现和Fragment实例的概念感到困惑。 You can use the same Fragment implementation in different Activity , but for each Activity you need a new Fragment instance. 您可以在不同的Activity使用相同的Fragment实现,但是对于每个Activity您都需要一个新的Fragment实例。 The lifecycle of that instance is what will be directly affected by the host Activity 's lifecycle. 该实例的生命周期将直接受到宿主Activity生命周期的影响。

Having a Fragment , let's call it FragmentA , and a couple Activity , let's call it ActivityA and ActivityB , you have 3 classes: 有一个Fragment ,我们称它为FragmentA ,还有一对Activity ,我们称它为ActivityAActivityB ,您有3个类:

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 . 在这种情况下,您可以在摊位ActivityAActivityB使用FragmentA的实现,但是对于每种情况,您都需要创建一个新的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 . 这样, instanceA将与ActivityA的生命周期相关,而instanceB将与ActivityB的生命周期相关,但Booth是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 ). 重用意味着某个FragmentFragmentA )不与某个ActivityActivityA )绑定,但是可以由不同的ActivitiesActivityBActivityC )使用。 It doesn't mean that you can pass a Fragment instance between Activities . 这并不意味着您可以在Activities之间传递一个Fragment实例。

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. 对于新活动,有一个新的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. 此处的键是Fragment的onAttach(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). 您只能重用要执行事务(活动或片段)的FragmentManager的现有片段的片段。

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. 请参阅来自FragmentManager的帖子: https : //developer.android.com/reference/android/app/FragmentManager.html如果在事务中调用addToBackStack方法,则BackStack中将允许使用碎片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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