Am I using fragments correctly?

I'm a little confused about what is the ideologically correct way to use fragments.

According to Android developers,

A snippet is a behavior or part of the user interface in an Event. You can combine several fragments in one action with create a multi-level interface and reuse a fragment in several actions. You can imagine a fragment as a modular section of an activity that has its own life cycle, receives its own input events, and which you can add or remove during work (sort of like a β€œsub activity" that you can use in different actions).

And:

Fragments decompose application functionality and user interface into reusable modules Add several fragments to the screen to avoid switching activities

And my use of fragments is as follows: I have only one main activity and a whole bunch of fragments. Instead of starting, I prefer replacing fragments. For example, I have FeedsFragment , FriendsFragment , MessagesFragment , and when I select something from the sliding menu, my Activity just replaces the main Fragment . If I started a fragment from another fragment, I put the previous one in the backstack.

Some fragments require action to change the action bar, and I do it directly with

 ((MainActivity)getActivity()).setupActionBar(); 

I currently do not have code that supports tablet tablets (as shown in the examples for Android developers), but I plan to add it.

So is this the right way to do something? Or am I completely missing something?

+4
source share
1 answer

As you know, a fragment has its own life cycle, and you can use its event from the lifecyle life whenever you want.

But the life cycle of fragments depends on the life cycle of activity. Therefore, when an action is destroyed, fragments are also destroyed.

 // Create new fragment and transaction Fragment newFragment = new ExampleFragment(); FragmentTransaction transaction = getFragmentManager().beginTransaction(); // Replace whatever is in the fragment_container view with this fragment, // and add the transaction to the back stack transaction.replace(R.id.fragment_container, newFragment); transaction.addToBackStack(null); // Commit the transaction transaction.commit(); 

Yo can use fragment transaction to replace them in one action, I think you are using the same,

I think that you are mistaken, and it makes no sense to use a fragment instead of using different types of activity.

But you have to think, you really have to use them in one activity? If you do not need to show them in one action, you do not need to use a snippet.

A fragment must always be embedded in activity, and a fragment life cycle directly depends on the host life cycle. For example, when an action is paused, all fragments in it and when the action is destroyed, as well as all fragments. However, although the action is being performed (it is in the state of a renewed life cycle), you can independently process each fragment, for example, add or remove them. When you perform such a fragment transaction, you can also add it to the back, which is controlled by activity - each entry of the back stack into the activity is a record of the fragment transaction that has occurred. The back stack allows the user to cancel a fragment transaction (move backward) by clicking the back button.

As a result, this is not so, but I think that if you do not need to, creating another activity is easy to support.

enter image description here

0
source

All Articles