简体   繁体   中英

Am I using fragments right?

I am a bit confused about what is the ideologically correct way of using fragments.

As the Android Developers states,

A Fragment represents a behavior or a portion of user interface in an Activity. You can combine multiple fragments in a single activity to build a multi-pane UI and reuse a fragment in multiple activities. You can think of a fragment as a modular section of an activity, which has its own lifecycle, receives its own input events, and which you can add or remove while the activity is running (sort of like a "sub activity" that you can reuse in different activities).

And also:

Fragments decompose application functionality and UI into reusable modules Add multiple fragments to a screen to avoid switching activities

And my usage of fragments goes the following way: I have only one main Activity and a whole bunch of fragments. Instead of starting activities, I prefer replacing fragments. For example, I have FeedsFragment , FriendsFragment , MessagesFragment , and when I select something from sliding menu, my Activity just replaces the main Fragment . If I'm launching a Fragment from other Fragment, I put the previous one in backstack.

Some fragments require the Activity to change the actionbar, an I do it directly by

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

Currently I don't have any code that supports tablet layouts (as seen in examples on android developers), but I'm planning to add it.

So, is this the right way of doing things? Or am I completely missing something?

As you know fragment has their own lifecycle, and you can use its event when ever you want from the activity lifecyle.

But Fragments lifecycle depens on activity lifecycle So when actvity destroyed, fragments destroyed also.

// 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 the fragment transaction, to replace them in one activity, I think you use the same way,

i think you arent wrong way and there are no problem to use fragment instead of using different activites.

But you should think about, you realy need to use them in one activty ? If you dont need to show them in one activity you dont need to use fragment.

A fragment must always be embedded in an activity and the fragment's lifecycle is directly affected by the host activity's lifecycle. For example, when the activity is paused, so are all fragments in it, and when the activity is destroyed, so are all fragments. However, while an activity is running (it is in the resumed lifecycle state), you can manipulate each fragment independently, such as add or remove them. When you perform such a fragment transaction, you can also add it to a back stack that's managed by the activity—each back stack entry in the activity is a record of the fragment transaction that occurred. The back stack allows the user to reverse a fragment transaction (navigate backwards), by pressing the Back button.

As a result , it is not wrong , but i think if you dont need, creating different activity is easy to maintain.

在此输入图像描述

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