Android Note 1: android Note 1

Source: Internet
Author: User

Android Note 1: android Note 1


1. Fragment Creation
Extents Fragment
View rootView = inflater. inflate (R. layout. fragment_main, container, false );
RootView. findViewById (R. id. ShowAnotherFragment). setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View v ){
GetFragmentManager (). beginTransaction ()
. AddToBackStack (null)
. Replace (R. id. fragment, new AnotherFragment ())
. Commit ();
}
});

2. Use of Intent
The intention is to pass the data of the previous activity to the next activity,
Intent. putExtra ("msg", data );

String data = intent. getStringExtra ("msg ");
Use intent to return data of the next activity to the previous activity

Intent. putExtra ("msg", "Hello FirstActivity ");
SetResult (RESULT_ OK, intent );

StartActivityForResult (intent1, 1 );
Protected void onActivityResult (){
Switch (requestCode ){
Case 1: if (resultCode = RESULT_ OK ){
} Break ;}

Public static interface NavigationDrawerCallbacks {
/**
3. LearnFragment
1. The MainActivity class loads an activity_main.xml file. If there is no data on the interface, load a MainActivityFragment class;
2. The MainActivity class loads a fragment_main.xml file. There are two buttons on the interface. One button shows another Fragment, and the other button opens the sidebar (SliderActivity class );
3. The onCreate () of the SliderActivity class loads an activity_slider.xml interface, which contains the FrameLayout layout and the custom fragment layout. FrameLayout can overlap as the background,
Fragment loads the NavigationDrawerFragment class.
The onCreateView () of the SliderActivity class loads the fragment_slider.xml interface and overwrites the FrameLayout layout in the activity_slider.xml interface.
4. In the onCreateView () method of the NavigationDrawerFragment class, create View. inflate to fill in an R. layout. slider_content interface,
The slider_content.xml interface has a button to implement the jump function by clicking the button,
Create a NavigationDrawerFragment interface in the NavigationDrawerCallbacks class and write an onGoToJkxuClicked () method.
Finally, implement the NavigationDrawerCallbacks interface in the SliderActivity class, and re-implement the onGoToJkxuClicked () method. In the method, implement the switching of fragments.

4. Meanings of the three parameters of the LayoutInflater class inflate (int resource, ViewGroup root, boolean attachToRoot) Method

Resource: the id of the layout file to be loaded, which means that the layout file needs to be loaded into the Activity for operation.
Root: the root control that needs to be appended to the resource file. What does it mean that inflate () will return a View object,
If the third parameter attachToRoot is true, the root object is returned as the root object. Otherwise, the root object's LayoutParams attribute is only appended to the root layout object of the resource object,
That is, the View on the outermost layer of the Layout file resource, for example, a LinearLayout or other Layout objects.
AttachToRoot: whether to attach root to the root View of the layout File

5. How to Use Fragment in an Activity

Create a fragment. xml --- fragment (Fragment) --- activity. xml layout nested fragment --- activity call the getFragmentManager (). findFragmentById (R. id. fragment) Method

6. A news content page
1. Create a news class
2. Create a news list layout and an adapter for news list layout;
3. Create a news content layout and a fragmented news content layout;
4. Create a layout for displaying news content (you can reference the layout of previous fragmented news content) and create an activity class to display news content;
5. Create a layout that displays the news list, and create a fragment to load the layout {
Initialize the onAttach () method data to complete the 2 adapter operation;
In the onCreateView () method, load the layout of the displayed News list to display the news list. In the News list, setOnItemClickListener (), transfer data in the method, and start three activity classes;
}
7. Summary of Fragment
1. fragments used during the activity
Fragment fragment = (Fragment) getFragmentManger (). findFragmentById (R. id. fragment );
2. Get activity in fragments
MainActivity activity = (MainActivity) getActivity ()
3. Fragment obtains another fragment through activity.
8. BroadcastReceiver
1.
Send standard broadcast sendBroadcast (intent) globally );
Globally send ordered broadcast sendOrderedBoadcast (intent, null)
Globally accept standard broadcast AndroidManifest. xml registration <receiver ER> <intent-filter> <action android: name = "com, example. broadcasttest. MY_BROADCAST">
Globally accept ordered broadcast with priority <intent-filter android: priority>
2.
In-app Broadcast
Send broadcast: sendBroadcast (intent)
Register broadcast listener: registerReceiver (BroadcastReceiver instance, IntentFilter instance)
9. FLAG of Activity Intent
1. Intent. FLAG_ACTIVITY_NEW_TASK to retrieve a stack activity or recreate an activity.
2. FLAG_ACTIVITY_SINGLE_TOP is equivalent to singletop in activity mode.
3. FLAG_ACTIVITY_CLEAR_TOP is equivalent to SingleTask in active mode.
10. Activity has four loading modes: standard (default), singleTop, singleTask, and singleInstance
SingleTop activity at the top of the stack will not be re-created
SingleTask is not on the top of the stack. It creates a unique active instance.
SingleInstance is a single instance. When other programs call the activity, a separate return stack is re-created.
11.
I. Three methods for obtaining the LayoutInflater instance (for example, LayoutInflater. from (context). inflate (R. layout. title, this ))

1. LayoutInflater inflater = getLayoutInflater (); // call the getLayoutInflater () of the Activity ()

2. LayoutInflater localinflater = (LayoutInflater) context. getSystemService (Context. LAYOUT_INFLATER_SERVICE );

3. LayoutInflater inflater = LayoutInflater. from (context );

II. Inflate Method

Public View inflate (int Resourece, ViewGroup root)

Purpose: Fill in a new view hierarchy from the specified XML resource file.

ReSource: The layout ID of the View.

Root: root View of the generated hierarchy
12. Load the simple View or use Fragment to load the View.
For example, if you use view, a lot of logic code may need to be written in the Activity. If there are many views.
High coupling. Fragment can be managed separately. It can be considered as a decoupling.
12.
Data storage: file storage SharePreference and database storage
1. Save the file storage
FileOutputStream out = openFileOutput ("file name", Context. MODE_PRIVATE );
BufferedWriter writer = new BufferdeWriter (new OutputStreamWriter (out ));
Writer. wreter ("111111111111111111 -- data ");

2. Read File Storage
FileInputStream in = openFileInput ("file name ");
BufferedRead read = new BufferedReader (new InputStreamReader (in ));
StringBuilder content = new StringBuilder ();
Content. append (reader. readLine ())
3. Save SharePreference
SharePreference. Editor editor = getSharedPreferences ("file name", MODE_PRIVATE.edit ());
Editor. putString ("KEY", "value ")
Editor. commit ();
4. Read SharePreference
SharePreferences pref = getSharedPreference ("file name", MODE_PRIVATE );
String string = pref. getString ("key", "Default Value ")
5. SQLiteOpenHelper db
Create Database db.exe cSQL ("create table person (id integer primary key, name text, age integer )")

6. When SharePreference is used, data is either completely completed or not completed.
SQLiteDatebase db = (new SQLiteOpenHelper). getWritableDatabase ();
Da. beginTranscation (); // starts a transaction
ContentValues values = new ContentValues ();
Values. put ("name", "xxx ");
Db. insert ("Database Name", null, values)
Db. setTransactionSuccessful // transaction enabled successfully
Db. endTransaction (); // transaction end

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.