Android Fragment details, including potential problems

Source: Internet
Author: User

Android Fragment details, including potential problems

Fragment is a new feature of the android v4 package named "fragmented layout". The layout aims to replace the obsolete tabhost, which makes operations more convenient and greatly increases developers' efficient development. When using Fragment, you must first master its lifecycle. Not much nonsense. See.

 
1. How to implement a menu at the bottom like QQ?
First, if you use a V4 package,
1. First, the parent class must inherit FragmentActivity.
2. Perform Initialization
It is not clear for new users.
Let me talk about the process first,
(1. To enable the fragmented layout management area, the first thing to do is to getActivity in fragment)
 
FragmentManager mFragmentManager= getFragmentManager();
(2. Start transactions through the fragmented layout manager, which is a key step for fragment execution.
FragmentTransaction transaction = mFragmentManager.beginTransaction();
(3. Then add the fragment layout to the transaction. This is the most critical step.

 

 
 
transaction.add(R.id.content, messageFragment);
(4 at last, you must not forget to commit the transaction. Otherwise, everything you do will be in vain.
 
 
transaction.commit();

 
3. Why does someone say this? OnRestoreInstanceState after reading the complete fragment lifecycle, you will understand that this will play a role in replying to data when fragment is not properly closed and opened. If this is not done, data may overlap. Of course, there are many ways to solve data overlap, so we will not introduce it here. Message for source code.
 
 
 
 
Package com. fragment. bottom; 



Import android. app. Activity;
Import android. app. FragmentManager;
Import android. app. FragmentTransaction;
Import android. graphics. Color;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. view. Window;
Import android. widget. ImageView;
Import android. widget. TextView;


Import com. example. networks. R;


/**
* The main Activity of the project. All Fragment is embedded here.
*/
Public class MainActivity extends Activity implements OnClickListener {


/**
* Fragment used to display messages
*/
Private MessageFragment mMessageFragment;


/**
* Displays the Fragment of a contact.
*/
Private ContactsFragment mContactsFragment;


/**
* Used to display dynamic Fragment
*/
Private NewsFragment mMewsFragment;


/**
* Displays the configured Fragment.
*/
Private SettingFragment mSettingFragment;


/**
* Message interface Layout
*/
Private View mMessageLayout;


/**
* Contact page layout
*/
Private View contactsLayout;


/**
* Dynamic interface Layout
*/
Private View mNewsLayout;


/**
* Set the interface Layout
*/
Private View mSettingLayout;


/**
* Display the control of the message icon in the Tab Layout
*/
Private ImageView messageImage;


/**
* Display the control of the contact icon in the Tab Layout
*/
Private ImageView contactsImage;


/**
* Controls for displaying dynamic icons in the Tab Layout
*/
Private ImageView newsImage;


/**
* Display the control for setting icons in the Tab Layout
*/
Private ImageView settingImage;


/**
* Display the Message Title control in the Tab Layout
*/
Private TextView mMessageText;


/**
* Display the contact title control in the Tab Layout
*/
Private TextView mContactsText;


/**
* Controls for displaying dynamic titles in the Tab Layout
*/
Private TextView newsText;


/**
* Display the title control in the Tab Layout
*/
Private TextView settingText;


/**
* Used to manage Fragment
*/
Private FragmentManager mFragmentManager;
// Record the location of Fragment
Private int position = 0;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
RequestWindowFeature (Window. FEATURE_NO_TITLE );
SetContentView (R. layout. activity_main_fragment );
// Initialize the layout element
InitViews ();
MFragmentManager = getFragmentManager ();
// Select 0th tabs at the first startup
SetTabSelection (position );
}
@ Override
Protected void onRestoreInstanceState (Bundle savedInstanceState ){
Position = savedInstanceState. getInt ("position ");
SetTabSelection (position );
Super. onRestoreInstanceState (savedInstanceState );
}
@ Override
Protected void onSaveInstanceState (Bundle outState ){
// Record the current position
OutState. putInt ("position", position );

}
/**
* Obtain the instances of the required controls and set the necessary click events for them.
*/
Private void initViews (){
MMessageLayout = findViewById (R. id. message_layout );
ContactsLayout = findViewById (R. id. contacts_layout );
MNewsLayout = findViewById (R. id. news_layout );
MSettingLayout = findViewById (R. id. setting_layout );
MessageImage = (ImageView) findViewById (R. id. message_image );
ContactsImage = (ImageView) findViewById (R. id. contacts_image );
NewsImage = (ImageView) findViewById (R. id. news_image );
SettingImage = (ImageView) findViewById (R. id. setting_image );
MMessageText = (TextView) findViewById (R. id. message_text );
MContactsText = (TextView) findViewById (R. id. contacts_text );
NewsText = (TextView) findViewById (R. id. news_text );
SettingText = (TextView) findViewById (R. id. setting_text );
MMessageLayout. setOnClickListener (this );
ContactsLayout. setOnClickListener (this );
MNewsLayout. setOnClickListener (this );
MSettingLayout. setOnClickListener (this );
}
@ Override
Public void onClick (View v ){
Switch (v. getId ()){
Case R. id. message_layout:
// When you click the message tab, select the first tab.
Position = 0;
SetTabSelection (position );
Break;
Case R. id. contacts_layout:
// When you click the contact tab, select 2nd tabs.
Position = 1;
SetTabSelection (position );
Break;
Case R. id. news_layout:
// When you click the dynamic tab, select 3rd tabs.
Position = 2;
SetTabSelection (position );
Break;
Case R. id. setting_layout:
// When you click Set tab, select 4th tabs.
Position = 3;
SetTabSelection (position );
Break;
Default:
Break;
}
}

/**
* Set the Selected tab based on the input index parameter.
*
* @ Param index
* Subscript corresponding to each tab page. 0 indicates message, 1 indicates contact, 2 indicates dynamic, and 3 indicates setting.
*/
Private void setTabSelection (int index ){
// Clear the selected status before each selection
ClearSelection ();
// Start a Fragment transaction
FragmentTransaction transaction = mFragmentManager. beginTransaction ();
// Hide all Fragment first to prevent multiple Fragment entries from being displayed on the interface.
HideFragments (transaction );
Switch (index ){
Case 0:
// When the message tab is clicked, the image and text color of the control are changed.
MessageImage. setImageResource (R. drawable. message_selected );
MMessageText. setTextColor (Color. WHITE );
If (mMessageFragment = null ){
// If MessageFragment is empty, create one and add it to the interface.
MMessageFragment = new MessageFragment ();
Transaction. add (R. id. content, mMessageFragment );
} Else {
// If MessageFragment is not empty, display it directly
Transaction. show (mMessageFragment );
}
Break;
Case 1:
// When the contact tab is clicked, the image and text color of the control are changed.
ContactsImage. setImageResource (R. drawable. contacts_selected );
MContactsText. setTextColor (Color. WHITE );
If (mContactsFragment = null ){
// If ContactsFragment is empty, create one and add it to the interface.
MContactsFragment = new ContactsFragment ();
Transaction. add (R. id. content, mContactsFragment );
} Else {
// If ContactsFragment is not empty, display it directly
Transaction. show (mContactsFragment );
}
Break;
Case 2:
// When a dynamic tab is clicked, the widget's image and text color are changed.
NewsImage. setImageResource (R. drawable. news_selected );
NewsText. setTextColor (Color. WHITE );
If (mMewsFragment = null ){
// If NewsFragment is empty, create one and add it to the interface.
MMewsFragment = new NewsFragment ();
Transaction. add (R. id. content, mMewsFragment );
} Else {
// If NewsFragment is not empty, display it directly
Transaction. show (mMewsFragment );
}
Break;
Case 3:
Default:
// When you click "Set tab", you can change the widget's image and text color.
SettingImage. setImageResource (R. drawable. setting_selected );
SettingText. setTextColor (Color. WHITE );
If (mSettingFragment = null ){
// If SettingFragment is empty, create one and add it to the interface.
MSettingFragment = new SettingFragment ();
Transaction. add (R. id. content, mSettingFragment );
} Else {
// If SettingFragment is not empty, display it directly
Transaction. show (mSettingFragment );
}
Break;
}
Transaction. commit ();
}


/**
* Clear all selected States.
*/
Private void clearSelection (){
MessageImage. setImageResource (R. drawable. message_unselected );
MMessageText. setTextColor (Color. parseColor ("# 82858b "));
ContactsImage. setImageResource (R. drawable. contacts_unselected );
MContactsText. setTextColor (Color. parseColor ("# 82858b "));
NewsImage. setImageResource (R. drawable. news_unselected );
NewsText. setTextColor (Color. parseColor ("# 82858b "));
SettingImage. setImageResource (R. drawable. setting_unselected );
SettingText. setTextColor (Color. parseColor ("# 82858b "));
}


/**
* Set all Fragment to hidden.
*
* @ Param transaction
* Transactions used to perform operations on Fragment
*/
Private void hideFragments (FragmentTransaction transaction ){
If (mMessageFragment! = Null ){
Transaction. hide (mMessageFragment );
}
If (mContactsFragment! = Null ){
Transaction. hide (mContactsFragment );
}
If (mMewsFragment! = Null ){
Transaction. hide (mMewsFragment );
}
If (mSettingFragment! = Null ){
Transaction. hide (mSettingFragment );
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.