Android Note One

Source: Internet
Author: User
Tags save file


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 ();
}
});

Use of 2.Intent
Use intent to pass data from the last activity to the next activity,
Intent.putextra ("MSG", data);

String data = Intent.getstringextra ("msg");
Use intent to return data from 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 For an example of fragment's passing in an activity (learnfragment), understand
1. The Mainactivity class loads a activity_main.xml, and if the interface does not have data, load a mainactivityfragment class;
2. Mainactivity class loads a fragment_main.xml with two buttons, one button displays another fragment, and the other button opens sidebar activity (Slideractivity Class);
3. The OnCreate () of the Slideractivity class loads a activity_slider.xml interface with Framelayout layout and custom fragment layout, framelayout can overlap as a background, The
fragment loads the Navigationdrawerfragment class. The
Slideractivity class Oncreateview () loads the Fragment_slider.xml interface, which overrides the Activity_slider.xml interface with a framelayout layout.
4. In the Oncreateview () method of the Navigationdrawerfragment class, create a view view.inflate to populate a r.layout.slider_content interface,
Slider_ Content.xml interface has a button to achieve the button click Jump Function,
and then in the Navigationdrawerfragment class to create a navigationdrawercallbacks interface, Write a ongotojkxuclicked () method.
finally implement the Navigationdrawercallbacks interface in the Slideractivity class, re-ongotojkxuclicked () method, the method to implement the fragmentation of the specific switch.

4. About the meaning of three parameters of Layoutinflater class inflate (int resource, ViewGroup root, Boolean Attachtoroot) method

Resource: The ID of the layout file needs to be loaded, meaning that the layout file needs to be loaded into the activity to operate.
Root: The root control that needs to be attached to the resource resource file, what does it mean that inflate () returns a View object,
If the third argument attachtoroot true, this root is returned as the root object, otherwise the Layoutparams property of the root object is simply appended to the root layout object of the resource object.
This is the outermost view of the layout file resource, such as a linearlayout or other layout object.
Attachtoroot: Whether to attach root to the root view of the layout file

5. How to use Fragment in activity

Create a fragment.xml---fragment (fragment)---activity.xml layout nested fragment---activity call Getfragmentmanager (). Findfragmentbyid (R.id.fragment) method

6. A News content page
1. Create a news category
2. Create a news list layout and create an adapter for the News list layout;
3. Create a news content layout, create a fragment to load the news content layout;
4. Create a display news content layout (can refer to the previous fragmented news content layout), create an activity class to display the news content;
5. Create a display news list layout, create a fragment to load this layout {
The initialization of the Onattach () method data completes the 2 adapter operation;
Then in the Oncreateview () method, load the display news list layout, display the news list; Then the news list Setonitemclicklistener (), passes the data in the method, and launches the 3 activity class;
}
7. Summary of Fragment
1 Usage Fragmentation in activity
Fragment Fragment = (Fragment) Getfragmentmanger (). Findfragmentbyid (r.id.fragment);
2 Getting active in fragments
Mainactivity activity = (mainactivity) getactivity ()
3, debris gets another fragment through activity
8. Broadcastreceiver
1.
Global Send standard broadcast sendbroadcast (intent);
Global send ordered broadcast sendorderedboadcast (intent,null)
Global accept standard broadcast Androidmanifest.xml registration <receiver> <intent-filter><action android:name= "com, Example.broadcasttest.MY_BROADCAST ">
Global accept ordered broadcast plus priority <intent-filter android:priority>
2.
In-app broadcasts
Send broadcast: Sendbroadcast (Intent)
Registering a Broadcast listener: Registerreceiver (broadcastreceiver instance, intentfilter instance)
9. Flag of Activity Intent
1.intent.flag_activity_new_task take out a stack of activities or recreate an activity
2.flag_activity_single_top equivalent to Singletop in active mode
3.flag_activity_clear_top equivalent to Singletask in active mode
Activity has four loading modes: standard (default), Singletop, Singletask, and SingleInstance
Singletop activities at the top of the stack are not recreated
Singletask not on top of the stack, creating a unique activity instance
SingleInstance Single instance, re-create a separate return stack when other programs invoke the activity
11.
One. There are three ways to get layoutinflater instances (such as layoutinflater.from (context). Inflate (R.layout.title, this))

1.LayoutInflater inflater = Getlayoutinflater (); Call activity's Getlayoutinflater ()

2.LayoutInflater Localinflater = (layoutinflater) context.getsystemservice (Context.layout_inflater_service);

3. Layoutinflater Inflater = layoutinflater.from (context);

Two. Inflate method

Public View Inflate (int resourece,viewgroup root)

Role: Populates a new view hierarchy from the specified XML resource file

The ID of the Resource:view layout

Root: Root view of the resulting hierarchy
12. The simple view load or use fragment load view
For example, you use view, many logic code may need to write in the activity, if the view is many. The
coupling degree is very high. The fragment can be managed separately and can be viewed as a decoupling bar
.
Data storage: File storage sharepreference and database storage
1. Save 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.SharePreference Save
    Sharepreference.editor Editor = getsharedpreferences ("filename", Mode_private.edit ());
    editor.putstring ("KEY", "value")
    editor.commit ();
4.SharePreference Read
    sharepreferences pref = getsharedpreference ("file name", mode_private);
    string string = pref.getstring ("Key", "Default value")
5.SQLiteOpenHelper DB
  CREATE database Db.execsql (" CREATE table person (ID integer primary key, name Text,age integer) ")

6.SharePreference use things, the data is either complete or not complete.
Sqlitedatebase db = (new Sqliteopenhelper). Getwritabledatabase ();
Da.begintranscation ();//Open things
Contentvalues values = new Contentvalues ();
Values.put ("name", "xxx");
Db.insert ("Database name", null,values)
db.settransactionsuccessful//Transaction Open successfully
Db.endtransaction ();//End of transaction

Android Note One

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.