Fragment回调接口应用间分享数据

package com.example.mydemo;

import java.util.List;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.os.Build;
import android.provider.ContactsContract.CommonDataKinds.Phone;

public class MainActivity extends Activity implements
        PlaceholderFragment.OnDtaReceivedListener {
    String sharedText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).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
            }
        }

    }

    void handleSendText(Intent intent) {
        sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
    }

    @Override
    public boolean onCreateOptionsMenu(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 boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onTextSelected(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 will be 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 be called from a separate thread to
    // avoid blocking
    // // your app's UI thread. (For simplicity of the sample, this code doesn't
    // do 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

package com.example.mydemo;

import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


/**
 * A placeholder fragment containing a simple view.
 */
public class PlaceholderFragment extends Fragment {

    OnDtaReceivedListener mCallback;

    // Container Activity must implement this interface
    public interface OnDtaReceivedListener {
        public void onTextSelected(View view);
    }
    
    public PlaceholderFragment() {
    }
  
    @Override
    public void onAttach(Activity activity) {
        // TODO Auto-generated method stub
        super.onAttach(activity);
        try {
            mCallback = (OnDtaReceivedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnDtaReceivedListener");
        }
    }




    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        
        TextView tv = (TextView) rootView.findViewById(R.id.tv);
        mCallback.onTextSelected(tv);
        return rootView;
    }
}

manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.mydemo"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="14"
        android:targetSdkVersion="19" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/CustomActionBarTheme" >
        <activity
            android:name="com.example.mydemo.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND" />

                <category android:name="android.intent.category.DEFAULT" />

                <data android:mimeType="text/plain" />
            </intent-filter>
        </activity>
    </application>

</manifest>

 

posted @ 2015-07-26 10:32  疾风剑  阅读(377)  评论(0编辑  收藏  举报