Fragment callback interface sharing data between applications

Source: Internet
Author: User

 PackageCom.example.mydemo;Importjava.util.List;Importandroid.app.Activity;ImportAndroid.app.ActionBar;Importandroid.app.Fragment;Importandroid.content.Intent;ImportAndroid.content.pm.PackageManager;ImportAndroid.content.pm.ResolveInfo;ImportAndroid.database.Cursor;ImportAndroid.net.Uri;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.Menu;ImportAndroid.view.MenuItem;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.TextView;ImportAndroid.os.Build;ImportAndroid.provider.ContactsContract.CommonDataKinds.Phone; Public classMainactivityextendsActivityImplementsPlaceholderfragment.ondtareceivedlistener {String sharedtext; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); if(Savedinstancestate = =NULL) {Getfragmentmanager (). BeginTransaction (). Add (R.id.container,Newplaceholderfragment ()). commit (); } Intent Intent=getintent (); String Action=intent.getaction (); String type=Intent.gettype (); if(Intent.ACTION_SEND.equals (ACTION) && type! =NULL) {            if("Text/plain". Equals (Type) {Handlesendtext (intent);//Handle text being sent            }        }    }    voidHandlesendtext (Intent Intent) {Sharedtext=Intent.getstringextra (Intent.extra_text); } @Override Public BooleanOncreateoptionsmenu (Menu menu) {//inflate the menu; This adds items to the action bar if it is present.getmenuinflater (). Inflate (r.menu.main_activity_actions, menu); return true; } @Override Public Booleanonoptionsitemselected (MenuItem item) {//Handle Action Bar item clicks here. The Action Bar would//automatically handle clicks on the Home/up button, so long//As you specify a the parent activity in Androidmanifest.xml.        intID =Item.getitemid (); if(id = =r.id.action_settings) {            return true; }        return Super. onoptionsitemselected (item); } @Override Public voidontextselected (view view) {//TODO auto-generated Method Stub        if(Sharedtext! =NULL) {            //Update UI to reflect text being shared( (TextView) view). SetText (Sharedtext); }    }    //@Override//protected void Onactivityresult (int requestcode, int resultcode, Intent//data) {    // //TODO auto-generated Method Stub//if (Requestcode = = 1) {// //Make sure the request was successful//if (ResultCode = = RESULT_OK) {// //Get the URI that points to the selected contact//Uri Contacturi = Data.getdata (); // //We only need the number column, because there would be is only one row in//The result//string[] projection = {Phone.number}; //    // //Perform the query on the contact to get the number column// //We don ' t need a selection or sort order (there ' s only one result for//The given URI)// //caution:the Query () method should is called from a separate the thread to//Avoid blocking// //your app ' s UI thread. (For simplicity of the sample, this code doesn ' t//Does that .) // //consider using Cursorloader to perform the query. //cursor cursor = getcontentresolver ()//. Query (Contacturi, projection, NULL, NULL, NULL); //Cursor.movetofirst (); //    // //Retrieve The phone number from the number column//int column = Cursor.getcolumnindex (phone.number); //String number = cursor.getstring (column); //Tv.settext (number); // //Do something with the phone number ...// }    // }    // }}

Fragment

 PackageCom.example.mydemo;Importandroid.app.Activity;Importandroid.app.Fragment;ImportAndroid.os.Bundle;ImportAndroid.view.LayoutInflater;ImportAndroid.view.View;ImportAndroid.view.ViewGroup;ImportAndroid.widget.TextView;/*** A placeholder fragment containing a simple view.*/ Public classPlaceholderfragmentextendsFragment {Ondtareceivedlistener mcallback; //Container Activity must implement this interface     Public InterfaceOndtareceivedlistener { Public voidontextselected (view view); }         Publicplaceholderfragment () {} @Override Public voidOnattach (activity activity) {//TODO auto-generated Method Stub        Super. Onattach (activity); Try{mcallback=(Ondtareceivedlistener) activity; } Catch(classcastexception e) {Throw Newclasscastexception (activity.tostring ()+ "must implement Ondtareceivedlistener"); }} @Override PublicView Oncreateview (layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View R Ootview= Inflater.inflate (R.layout.fragment_main, container,false); TextView TV=(TextView) Rootview.findviewbyid (r.id.tv);        mcallback.ontextselected (TV); returnRootview; }}

Manifest

<?XML version= "1.0" encoding= "Utf-8"?><Manifestxmlns:android= "Http://schemas.android.com/apk/res/android" Package= "Com.example.mydemo"Android:versioncode= "1"Android:versionname= "1.0" >    <USES-SDKandroid:minsdkversion= "+"android:targetsdkversion= "+" />    <ApplicationAndroid:allowbackup= "true"Android:icon= "@drawable/ic_launcher"Android:label= "@string/app_name"Android:theme= "@style/customactionbartheme" >        <ActivityAndroid:name= "Com.example.mydemo.MainActivity"Android:label= "@string/app_name" >            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.MAIN" />                <categoryAndroid:name= "Android.intent.category.LAUNCHER" />            </Intent-filter>            <Intent-filter>                <ActionAndroid:name= "Android.intent.action.SEND" />                <categoryAndroid:name= "Android.intent.category.DEFAULT" />                <DataAndroid:mimetype= "Text/plain" />            </Intent-filter>        </Activity>    </Application></Manifest>

Fragment callback interface sharing data between applications

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.